【问题标题】:Adding minor ticks to pandas plot向熊猫图添加次要刻度
【发布时间】:2020-10-03 03:18:54
【问题描述】:

我有以下代码:

from pandas_datareader import data as web
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(2, 1)
df = web.DataReader('F', 'yahoo')
df2 = web.DataReader('Fb', 'yahoo')
ax = df.plot(figsize=(35,15), ax=ax1)
df2.plot(y = 'Close', figsize=(35,15), ax=ax2)

plt.show()

这会产生如下所示的图表:

如何更改 pandas 图中的次要刻度,使其生成 x 轴,如下所示:

【问题讨论】:

    标签: python python-3.x pandas datetime matplotlib


    【解决方案1】:

    检查此代码:

    from pandas_datareader import data as web
    import matplotlib.pyplot as plt
    import matplotlib.dates as md
    
    fig, (ax1, ax2) = plt.subplots(2, 1)
    df = web.DataReader('F', 'yahoo')
    df2 = web.DataReader('Fb', 'yahoo')
    ax = df.plot(figsize=(35,15), ax=ax1)
    df2.plot(y = 'Close', figsize=(35,15), ax=ax2)
    
    for ax in (ax1, ax2):
        ax.xaxis.set_major_locator(md.MonthLocator(bymonth = range(1, 13, 6)))
        ax.xaxis.set_major_formatter(md.DateFormatter('%b\n%Y'))
        ax.xaxis.set_minor_locator(md.MonthLocator())
        plt.setp(ax.xaxis.get_majorticklabels(), rotation = 0 )
    
    plt.show()
    

    您可以使用ax.xaxis 方法管理xticks。上面的代码产生了这个图:

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 2018-11-13
      • 2018-06-03
      • 2017-02-27
      • 1970-01-01
      相关资源
      最近更新 更多