Skip to content

Use data argument for scatter plotting timestamps from pandas #11391

Closed
@ImportanceOfBeingErnest

Description

@ImportanceOfBeingErnest

Bug report

Bug summary

This may be either a bug report or a feature request, depending how you view things. The issue is that you cannot use the data argument to scatter to plot a pandas dataframe Timestamp column.

Code for reproduction

Consider the following data

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np; np.random.seed(42)

df = pd.DataFrame(np.random.rand(10,2), columns=["x","y"])
df["time"] = pd.date_range("2018-01-01", periods=10, freq="D")

Using the data argument, you may plot

plt.plot("x", "y", data=df)

You may equally plot

plt.plot("time", "y", data=df)

You may also scatter

plt.scatter(x="x", y="y", data=df)

However, you may not scatter the timestamps

plt.scatter(x="time", y="y", data=df)

This fails with a TypeError: invalid type promotion

Traceback (most recent call last):
  File "D:/.../test/GI_scatterdataarg.py", line 40, in <module>
    plt.scatter(x="time", y="y", data=df)
  File "d:\...\matplotlib\lib\matplotlib\pyplot.py", line 2623, in scatter
    verts=verts, edgecolors=edgecolors, data=data, **kwargs)
  File "d:\...\matplotlib\lib\matplotlib\__init__.py", line 1775, in inner
    return func(ax, *args, **kwargs)
  File "d:\...\matplotlib\lib\matplotlib\axes\_axes.py", line 3982, in scatter
    offsets = np.column_stack([x, y])
  File "D:\...\matplotlib\lib\site-packages\numpy\lib\shape_base.py", line 369, in column_stack
    return _nx.concatenate(arrays, 1)
TypeError: invalid type promotion

This is a bit surprising since you may well plot timestamps through scatter

plt.scatter(x=df.time.values, y=df.x.values)

just not through the data argument.

Expected outcome

Given that one may plot the timestamps correctly with plot and timestamps with scatter by providing the values, it would be nice to be able to use the data argument with scatter as well.

... and ideally with bar, fill_between etc. ;-)

Matplotlib version

  • Operating system: Win 8.1
  • Matplotlib version: master 2.2.2.post1270+g6665bc739
  • Matplotlib backend: Qt5Agg
  • Python version: 3.6
  • Jupyter version (if applicable):
  • Other libraries: Pandas 0.22.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions