The Matplotlib library is a commonly used data visualization library that can help us display data visually. Among them, column chart is a common way of displaying data. When drawing a column chart, we can increase the beauty and readability of the chart by setting the color.
In Matplotlib, the color setting of the column chart can be achieved by setting the parameter color
. The specific methods and examples are as follows:
color
to a color value, the color of the entire column chart can be kept consistent. The following is a simple sample code: import matplotlib.pyplot as plt # 数据 x = [1, 2, 3, 4, 5] y = [10, 20, 15, 25, 30] # 绘制柱形图 plt.bar(x, y, color='blue') # 设置标题和标签 plt.title('柱形图示例') plt.xlabel('X轴') plt.ylabel('Y轴') # 显示图表 plt.show()
In the above code, the value of the parameter color
is 'blue'
, which represents the color of the column chart is blue.
color
to a color array. The length of the array must be the same as the column. The number is the same. The following is a sample code: import matplotlib.pyplot as plt # 数据 x = [1, 2, 3, 4, 5] y = [10, 20, 15, 25, 30] colors = ['red', 'green', 'blue', 'yellow', 'orange'] # 绘制柱形图 plt.bar(x, y, color=colors) # 设置标题和标签 plt.title('柱形图示例') plt.xlabel('X轴') plt.ylabel('Y轴') # 显示图表 plt.show()
In the above code, the value of the parameter color
is a color array ['red', 'green', 'blue' , 'yellow', 'orange']
, corresponding to the color of each column.
In addition, we can also use predefined color mapping to set the color of the column. Predefined color maps include 'b'
(blue), 'g'
(green), 'r'
(red), ' c'
(cyan), 'm'
(magenta), 'y'
(yellow), 'k'
(black), etc. Colors can also be specified by using RGB values, such as '#FF0000'
for red and '#00FF00'
for green.
In summary, by setting the parameters color
, we can draw column charts with different colors in the Matplotlib library. This can make the chart more beautiful and improve the readability and visualization of the data. Hope this article helps you!
The above is the detailed content of How to set column chart color in Matplotlib library. For more information, please follow other related articles on the PHP Chinese website!