Python 如何基于非零的其他值绘制.bar

Python 如何基于非零的其他值绘制.bar,python,matplotlib,Python,Matplotlib,通常,条形图将显示底部为零。改变底部时,杆向上或向下移动 import numpy as np import matplotlib.pyplot as plt N = 5 menMeans = (20, 35, 30, 35, 27) ind = np.arange(N) # the x locations for the groups width = 0.35 # the width of the bars: can also be len(x) sequence

通常,条形图将显示底部为零。改变底部时,杆向上或向下移动

import numpy as np
import matplotlib.pyplot as plt


N = 5
menMeans   = (20, 35, 30, 35, 27)

ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, menMeans, width, bottom=0,color='#d62728')

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend('Men')
plt.savefig('bar.png')
plt.show()
而我想要的不是上下移动。下面的代码显示零的底部

我想显示基于值(如25)的图表。如果数据为20,则在图表中25下方显示5。

您可以将
menMeans
转换为numpy,然后减去底部。减去25,示例数组将是
[-5,10,5,10,2]

可以通过
ax.spines['bottom']将x轴移动到该高度。设置位置(('data',bottom))
。类似地,可以使其他脊椎不可见(
ax.spines[…].set_color('none')

plt.tick_params()
可以通过将记号的长度设置为0来删除记号

ax.text(x,y,text)
可用于在给定位置设置文本。换行有助于获得独立于y轴的适当填充

将numpy导入为np
将matplotlib.pyplot作为plt导入
N=5
menMeans=(20,35,30,35,27)
menMeans=np.数组(menMeans)
ind=np.arange(N)#组的x个位置
宽度=0.35#钢筋宽度:也可以是len(x)序列
底部=25
p1=plt.bar(ind,Men表示底部,宽度,底部=底部,颜色='d62728',标签='Men')
plt.ylabel(“分数”)
项目名称(“按组和性别划分的分数”)
plt.xticks(ind,('G1','G2','G3','G4','G5'))
plt.yticks(np.arange(0,81,10))
plt.tick_参数(轴=两个’,长度=0)
ax=plt.gca()
斧头刺['left'].设置颜色(“无”)
斧头刺['right'].设置颜色('none')
ax.spines['bottom'].设置_位置(('data',bottom))
斧头刺['top'].设置颜色(“无”)
对于我来说,zip的意思是(ind,menMeans):
text(我的意思是,如果mean>=bottom-else f'\n{mean}',ha='center',va='center',则f'{mean}\n'
plt.legend()
plt.savefig('bar.png'))
plt.show()