Apr-03-2022, 04:05 PM
I'm creating a new thread here, which originated in a question about floor division and tick labels. In there, Gribouillis suggested using np.linspace() for the axis. How would I do that with datetimes?
Here's some code:
Another problem is that if I want more tick labels and change that third argument of pd.date_range() to 15, I get: ValueError: x and y must have same first dimension, but have shapes (15,) and (5,) . That makes sense because I've entered something in the x-coordinate place that doesn't match up with y-coordinates. Really, this date_range is intended to be used only for tick labels that need not correspond with x-coordinates so I probably need to specify a dedicated x-coordinate list for plt.plot() and then create something to be used for [customizable] evenly-spaced datetime tick labels.
Any suggestions as to how I could do this?
Here's some code:
import pandas as pd import matplotlib.pyplot as plt sample_data = [3, 6, 2, 12, 15] e = pd.date_range('2010-01-01', '2011-12-31',5) print(e) print(type(e)) print(list(e)) plt.plot(e,sample_data) plt.xticks(rotation=90) plt.show()I see a few issues here. First, doing this gives me nine x-tick labels from 2010-01 to 2011-01. It doesn't give 5, which is what I wanted (third argument in L5), and the format isn't what I wanted (yyyy-mm-dd as contained in the pd.date_range() line (why?).
Another problem is that if I want more tick labels and change that third argument of pd.date_range() to 15, I get: ValueError: x and y must have same first dimension, but have shapes (15,) and (5,) . That makes sense because I've entered something in the x-coordinate place that doesn't match up with y-coordinates. Really, this date_range is intended to be used only for tick labels that need not correspond with x-coordinates so I probably need to specify a dedicated x-coordinate list for plt.plot() and then create something to be used for [customizable] evenly-spaced datetime tick labels.
Any suggestions as to how I could do this?