代码之家  ›  专栏  ›  技术社区  ›  Sam B.

熊猫从右到右绘制ylabels或从右到左绘制负值

  •  0
  • Sam B.  · 技术社区  · 7 年前

    我正在画一张熊猫的图表

    df9[(df9['Delivery Quality'] < 0) & (df9['year'] == 2016)][['country','Delivery Quality']].plot(x='country', y='Delivery Quality', kind='barh', figsize=(20,20), legend=False, secondary_y=False)
    

    它返回这个图

    enter image description here

    与正值不同,这使得将名称与列关联起来更加困难

    enter image description here

    解决方法是将ylabel向右移动,或者让负值的xvalues从右向左移动

    2 回复  |  直到 7 年前
        1
  •  1
  •   JARS    7 年前

    使用matplotlib轴' tick_right() :

    ax = frm.plot(x='country', y='Delivery Quality', kind='barh', figsize=(10,5), legend=False, secondary_y='country')
    ax.yaxis.tick_right()
    
        2
  •  0
  •   Gozy4    7 年前

    您可以这样做,在绘图的右侧创建第二个标签,并在左侧隐藏标签。

    ax = df9[(df9['Delivery Quality'] < 0) & (df9['year'] == 2016)][['country','Delivery Quality']].plot(x='country', y='Delivery Quality', kind='barh', figsize=(20,20), legend=False, secondary_y=False)
    ax2 = ax.twinx()
    ax.set_yticks([])
    ax2.set_yticks(df['country'])