How to Draw a Colored Histogram

How to Draw a Colored Histogram

    This article records how to draw a colored histogram

0. Main steps

    Above code:

import matplotlib.pyplot as plt
mean_values=[4385,6779,18235,4280,3154,7892]
x_pos=[1,2,3,4,5,6]
import matplotlib.colors as col
import matplotlib.cm as cm
cmap1=cm.ScalarMappable(col.Normalize(min(mean_values),max(mean_values),cm.hot))
cmap2=cm.ScalarMappable(col.Normalize(0,20,cm.hot))

bar1=plt.bar(x_pos,mean_values,color=cmap1.to_rgba(mean_values))
plt.colorbar(cmap1)  #根据换手率均值的大小显示颜色
plt.bar_label(bar1)
plt.show()

    The above renderings:

    By the way, to solve the problem that Chinese cannot be displayed in PyCharm, just add the following two lines of code:

import matplotlib

# plt.rcParams['font.sans-serif']=['SimHei']
# plt.rcParams['axes.unicode_minus']=False
matplotlib.rc("font", family='PingFang HK')//   因为是用的MacBook进行开发,所以这里是显示的苹果中文字体

    Links to the above references:
1. Matplotlib visualization of histogram plt.bar()
2. Matplotlib bar chart
3. The most complete Python bar chart (histogram)

Guess you like

Origin blog.csdn.net/weixin_43981621/article/details/129336016