代码之家  ›  专栏  ›  技术社区  ›  rcs

Matplotlib x轴消失

  •  -1
  • rcs  · 技术社区  · 7 年前

    我正在试验python的matplotlib函数,结果有些奇怪,x轴标签从绘图中消失了。

    我正在尝试以下示例,如下所示 Youtube : enter image description here

    在该示例中,x轴显示绘图的年份。 当我尝试在自己的Jupyter笔记本中进行操作时,我得到的结果如下:

    代码:

    yearly_average[-20:].plot(x='year', y='rating', figsize=(15,10), grid=True)
    

    enter image description here

    如何修复此问题?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Scott Boston    7 年前

    让我们将Yearal\u average数据帧中的Year列从str或object数据类型转换为整数。然后,使用熊猫图进行绘图。

    MVCE:

    使用xaxis ticklabels的工作示例,其中数据类型“Year”是整数

    df = pd.DataFrame({'Year':[2000,2001,2002,2003,2004,2005],'Value':np.random.randint(1000,5000,6)})
    
    df.plot('Year','Value')
    

    enter image description here

    现在,让我们把“年”选为str,再做一次测试。

    df1 = df.copy()
    df1['Year'] = df['Year'].astype(str)
    df1.plot('Year','Value')
    

    enter image description here