同一matplotlib图表上的负值栏 [英] Negative values bars on the same matplotlib chart

查看:63
本文介绍了同一matplotlib图表上的负值栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在同一图上显示3个条形图.但是,具有负值的条形存在一个问题,因为它们从顶部悬垂或从无处垂下.有什么想法让它看起来更好吗?

I am trying to display 3 bar charts on the same plot. There is an issue with bars that have negative values though, because they are hanging down either from the top or from nowhere. Any ideas how to make it look nicer?

import pandas as pd
import matplotlib.pyplot as plt

x = range(6)
a1 = [-1, -4, -3, -6, 2, 8]
a2 = [ 4, 12, 8, 1, 10, 9]
a3 = [100, 110, 120, 130, 115, 110]

df = pd.DataFrame(index=x, 
                  data={'A': a1, 
                        'B': a2, 
                        'C': a3})

fig, ax = plt.subplots()
ax2 = ax.twinx()
ax3 = ax.twinx()

ax3.spines["right"].set_position(("axes", 1.1))

df['A'].plot(ax=ax, kind='bar', color='blue', width=0.2, position=2)
df['B'].plot(ax=ax2, kind='bar', color='green', width=0.2, position=1)
df['C'].plot(ax=ax3, kind='bar', color='red', width=0.2, position=0)

推荐答案

您可以执行以下几项操作来使其更具可读性.您的大问题是,您拥有3个单独的y轴,因此很难分辨出哪个变量指向哪个变量,并且变量0线(从其定义条形)都很难识别.您可以先更改轴颜色以适合您的数据,以帮助提高可读性.然后,您要为所有y轴设置极限,以使它们的零线相同,并使用一些乘法因子来调整比例.不过请小心,因为这可能会误导读者,因为它们依赖于将"A"与"B"与"C"进行比较的物理意义.

There are a couple things you can do to make this more readable. Your big issue is that you have 3 separate y-axis so that its both hard to discern which goes to which variable and you have a variable zero line (which the bars are defined from). You can help the readability first by changing the axis colors to fit your data. Then you want to set your limits for all your y-axis so that they the zero line is the same and use some multiplication factor to then adjust your scales. Be careful though because this could mislead readers dependent on the physical significance of comparing 'A' to 'B' to 'C'.

import pandas as pd
import matplotlib.pyplot as plt

x = range(6)
a1 = [-1, -4, -3, -6, 2, 8]
a2 = [ 4, 12, 8, 1, 10, 9]
a3 = [100, 110, 120, 130, 115, 110]

df = pd.DataFrame(index=x, 
                  data={'A': a1, 
                        'B': a2, 
                        'C': a3})

fig, ax = plt.subplots()
ax2 = ax.twinx()
ax3 = ax.twinx()

ax3.spines["right"].set_position(("axes", 1.1))

df['A'].plot(ax=ax, kind='bar', color='blue', width=0.2, position=2)
df['B'].plot(ax=ax2, kind='bar', color='green', width=0.2, position=1)
df['C'].plot(ax=ax3, kind='bar', color='red', width=0.2, position=0)

#Set the limits based off your negative bar graph then multiply those by some factor
ax.set_ylim(df['A'].min()*1.1,df['A'].max()*1.1) 
ax2.set_ylim(df['A'].min()*2,df['A'].max()*2)
ax3.set_ylim(df['A'].min()*20,df['A'].max()*20)

#Change color of axis to make more readable
ax.tick_params(axis='y',color='blue',labelcolor='blue')
ax2.tick_params(axis='y',color='green',labelcolor='green')
ax3.tick_params(axis='y',color='red',labelcolor='red')

#Also add a limit to the x-axis to     
ax.set_xlim(-0.5)

plt.show()

这篇关于同一matplotlib图表上的负值栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆