Python 更改直方图中的条形图颜色
我想显示一个直方图与酒吧在不同的颜色根据条件。 我的意思是,我想用不同的颜色设置介于2和5之间的条 我试过这个:Python 更改直方图中的条形图颜色,python,matplotlib,Python,Matplotlib,我想显示一个直方图与酒吧在不同的颜色根据条件。 我的意思是,我想用不同的颜色设置介于2和5之间的条 我试过这个: bins = np.linspace(0, 20, 21) lista_float_C1 = [1,1,1,2,2,2,3,4,4,5,5,6,7,8,8,8,8,10,11,11] colors = [] y = plt.hist(lista_float_C1, bins, alpha=0.5 ) for x in y[1]: if (x >= 2)&
bins = np.linspace(0, 20, 21)
lista_float_C1 = [1,1,1,2,2,2,3,4,4,5,5,6,7,8,8,8,8,10,11,11]
colors = []
y = plt.hist(lista_float_C1, bins, alpha=0.5 )
for x in y[1]:
if (x >= 2)&(x=<5):
colors.append('r')
else:
colors.append('b')
print(colors)
plt.hist(lista_float_C1, bins, alpha=0.5, color = colors )
plt.show()
打印面片后,可以修改面片:
lista_float_C1 = [1,1,1,2,2,2,3,4,4,5,5,6,7,8,8,8,8,10,11,11]
fig,ax = plt.subplots()
ax.hist(lista_float_C1, bins, alpha=0.5 )
for p in ax.patches:
x = p.get_height()
# modify this to fit your needs
color = 'r' if (2<=x<=5) else 'b'
p.set_facecolor(color)
plt.show()
plt.show()
lista_float_C1=[1,1,1,2,2,2,3,4,4,5,5,6,7,8,8,8,10,11]
图,ax=plt.子批次()
ax.hist(列表A\u float\u C1,箱,alpha=0.5)
对于ax.patches中的p:
x=p.获取高度()
#修改此选项以满足您的需要
color='r'如果(2)但是我想把X=2,X=3,X=4和X=5的条设置为红色,在X轴上,我不介意它们的高度(Y轴)
lista_float_C1 = [1,1,1,2,2,2,3,4,4,5,5,6,7,8,8,8,8,10,11,11]
fig,ax = plt.subplots()
ax.hist(lista_float_C1, bins, alpha=0.5 )
for p in ax.patches:
x = p.get_height()
# modify this to fit your needs
color = 'r' if (2<=x<=5) else 'b'
p.set_facecolor(color)
plt.show()
plt.show()
for p in ax.patches:
# changes here
x,y = p.get_xy()
color = 'r' if (2<=x<=5) else 'b'
p.set_facecolor(color)
plt.show()