0

I have a Pandas dataframe with datetime elements ('date') and floats ('cups) and am wondering if there's anyway to use np.vstack() to stack the two.

Previously, I had converted the datetime elements to floats and used vstack() without issue. However, I am using xx to update a scatterplot with a slider and, since I would like the plot to use dates, can no longer use this method.

xx = np.vstack((df['date'], df['cups']))
scat.set_offsets(xx.T)

I see that there's been a similar question, but I'm not sure how to adapt the answer given there to my situation: Can ndarray store datetime and float?.

Since the end-goal is to call set_offsets with the right kind of structure, the solution need not use vstack. I'm just not sure how to get the structure set_offsets needs without using vstack.

sonny
  • 313
  • 3
  • 11
  • A numpy array can't store a mix of dtypes, unless you create a `structured` array. Why don't you stick with a dataframe which can have different dtypes in its columns? We don't what this offsets function is, or what it requires. – hpaulj Feb 05 '21 at 00:33
  • What does this offset function accept? – hpaulj Feb 05 '21 at 00:44
  • Sorry, assumed too much background knowledge. `set_offests` is a function of `ax.scatter`. The example linked [here](https://matplotlib.org/3.1.1/gallery/animation/rain.html) shows one use of it. I'm using it more along the lines of [this](https://stackoverflow.com/questions/38368990/scatter-plot-with-a-slider-in-python) example, which is why, @hpaulj, I'm not sure sticking with dataframes will work for my situation. Am I missing something? – sonny Feb 05 '21 at 01:44
  • What does `scat.get_offsets()` produce? How does that compare with the arrays which you used to create the scatter in the first place? – hpaulj Feb 05 '21 at 07:39
  • When I experiment with a simple `scatter` with a `dates` axis, `get_offsets` gives a masked array with float values. The dates have been converted to float values. I believe your `set_offsets` values need to do the same. – hpaulj Feb 05 '21 at 17:45

1 Answers1

0

Solved! Thanks to @hpaulj for the pointer. I was wrongly assuming that scat.set_offsets() needed datetime input for a plot with dates, so was resistant to passing floats. It turns out, however, that a date scatter plot can accept floats passed to set_offsets and still display the data correctly.

I've converted the column in question via the following:

df.loc[:, 'date'] = df['date'].map(lambda x: matplotlib.dates.date2num(x)).

sonny
  • 313
  • 3
  • 11