I am trying to generate a smaller figure visualising a pandas time series. The automatically-generated x-ticks, however, do not adapt to the new size and result in overlapping ticks. I am wondering how can I adapt the frequency of the x-ticks? Eg for this example:
figsize(4, 2)
num = 3000
X = linspace(0, 100, num=num)
dense_ts = pd.DataFrame(sin(X) + 0.1 * np.random.randn(num),
pd.date_range('2014-01-1', periods=num, freq='min'))
dense_ts.plot()
The figure that I get is:
I am able to work around this problem using the Matplotlib date plotting, but it is not a very elegant solution - the code requires me to specify all the output formatting on a per-case basis.
figsize(4, 2)
from matplotlib import dates
fig, ax = plt.subplots()
ax.plot_date(dense_ts.index.to_pydatetime(), dense_ts, 'b-')
ax.xaxis.set_minor_locator(dates.HourLocator(byhour=range(24),
interval=12))
ax.xaxis.set_minor_formatter(dates.DateFormatter('%H:%m'))
ax.xaxis.set_major_locator(dates.WeekdayLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n%a\n%Y'))
plt.show()
I'm wondering if there is a way to solve this issue using the pandas plotting
module or maybe by setting some axes
object properties? I tried playing with the ax.freq
object, but couldn't really achieve anything.
You can pass a list of x axis values you want displayed in your dense_ts.plot()
dense_ts.plot(xticks=['10:01','22:01'...])
Another example for clarity
df = pd.DataFrame(np.random.randn(10,3))
Plot without specifying xticks
df.plot(legend=False)
Plot with xticks argument
df.plot(xticks=[2,4,6,8],legend=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.