本文介绍了pandas .p​​lot()x轴刻度频率-如何显示更多刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pandas .p​​lot()绘制时间序列,并希望每个月以x-tick的形式显示.

I am plotting time series using pandas .plot() and want to see every month shown as an x-tick.

这是数据集结构

这是.plot()的结果

Here is the result of the .plot()

我正尝试使用其他帖子和matplotlib 文档中的示例,并做类似的事情

I was trying to use examples from other posts and matplotlib documentation and do something like

ax.xaxis.set_major_locator(
   dates.MonthLocator(revenue_pivot.index, bymonthday=1,interval=1))

但是,这消除了所有的滴答声:(

But that removed all the ticks :(

我也尝试通过xticks = df.index,但是它没有任何改变.

I also tried to pass xticks = df.index, but it has not changed anything.

在x轴上显示更多刻度线的正确方法是什么?

What would be the rigth way to show more ticks on x-axis?

推荐答案

无需将任何args传递给MonthLocator.确保根据@Rotkiv的答案在df.plot()调用中使用x_compat.

No need to pass any args to MonthLocator. Make sure to use x_compat in the df.plot() call per @Rotkiv's answer.

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import matplotlib.dates as mdates

df = pd.DataFrame(np.random.rand(100,2), index=pd.date_range('1-1-2018', periods=100))
ax = df.plot(x_compat=True)
ax.xaxis.set_major_locator(mdates.MonthLocator())
plt.show()

这篇关于pandas .p​​lot()x轴刻度频率-如何显示更多刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:08