CT Zhu..
103
简单,只需使用 .set_color
>>> barlist=plt.bar([1,2,3,4], [1,2,3,4])
>>> barlist[0].set_color('r')
>>> plt.show()
对于你的新问题,也没有那么难,只需要从轴上找到条形图,例如:
>>> f=plt.figure()
>>> ax=f.add_subplot(1,1,1)
>>> ax.bar([1,2,3,4], [1,2,3,4])
>>> ax.get_children()
[,
,
,
,
,
,
,
,
,
,
,
]
>>> ax.get_children()[2].set_color('r')
#You can also try to locate the first patches.Rectangle object
#instead of direct calling the index.
如果您有一个复杂的图并想要首先识别条形,请添加:
>>> import matplotlib
>>> childrenLS=ax.get_children()
>>> barlist=filter(lambda x: isinstance(x, matplotlib.patches.Rectangle), childrenLS)
[,
,
,
,
]
请注意,这将删除栏的黑色边框(边缘).它可以通过使用:`barlist [0] .set_edgecolor("black")`来恢复.更好的方法可能是使用`barlist [0] .set_facecolor('r')`,它不会改变条形的边缘. (6认同)