pandas .p​​lot()x轴刻度频率 - 如何显示更多刻度?

时间:2016-09-27 01:30:19

标签: pandas matplotlib

我正在使用pandas .p​​lot()绘制时间序列,并希望看到每个月显示为x-tick。

这是数据集结构 data set

这是.plot()

的结果

enter image description here

我试图使用其他帖子和matplotlib documentation中的示例并执行类似

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

但是删除了所有的滴答声:(

我也试图通过xticks = df.index,但它没有改变任何东西。

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

4 个答案:

答案 0 :(得分:7)

无需将任何参数传递给MonthLocator。请务必在@ {Rotkiv的答案x_compat来电中使用df.plot()

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()

答案 1 :(得分:3)

您还可以使用pandas <option value="1">1</option> <% if not $Quantity || $Quantity == 2 %> <option value="2">2</option> <% end_if %> <% if not $Quantity || $Quantity == 3 %> <option value="3">3</option> <% end_if %> <% if not $Quantity || $Quantity == 4 %> <option value="4">4</option> <% end_if %> <% if not $Quantity || $Quantity == 5 %> <option value="5">5</option> <% end_if %> <% if not $Quantity || $Quantity == 6 %> <option value="6">6</option> <% end_if %> <% if not $Quantity || $Quantity == 7 %> <option value="7">7</option> <% end_if %> <% if not $Quantity || $Quantity == 8 %> <option value="8">8</option> <% end_if %> <% if not $Quantity || $Quantity == 9 %> <option value="9">9</option> <% end_if %> <% if not $Quantity || $Quantity > 9 %> <option value="10">10</option> <% end_if %> 对象的属性手动格式化pandas DateTimeIndex的x轴刻度和标签。

我发现使用Timestamp的定位器比使用其他日期时间格式而不是大熊猫更容易(如果我没有记错的话),因此如果没有相应地转换日期,有时会显示奇怪的行为。

这是一个通用示例,它将每个月的第一天显示为基于pandas matplotlib.dates对象属性的标签:

Timestamp

的产率:

enter image description here

希望这有帮助!

答案 2 :(得分:2)

正确的方法here 使用x_compat参数,可以抑制自动刻度分辨率调整

df.A.plot(x_compat=True)

答案 3 :(得分:0)

如果只想显示更多的报价,也可以深入研究pd.plotting._converter的结构:

dai = ax.xaxis.minor.formatter.plot_obj.date_axis_info
dai['fmt'][dai['fmt'] == b''] = b'%b'

绘制后,formatterTimeSeries_DateFormatter,并且已经调用了_set_default_format,因此self.plot_obj.date_axis_info is not None。现在,您可以根据自己的喜好来操纵结构化数组.date_axis_info,即包含更少的b''和更多的b'%b'

相关问题