Home > Backend Development > Python Tutorial > How to set column chart color in Matplotlib library

How to set column chart color in Matplotlib library

王林
Release: 2024-01-17 10:02:11
Original
1108 people have browsed it

How to set column chart color in Matplotlib library

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:

  1. Single color setting
    By setting the parameter 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()
Copy after login

In the above code, the value of the parameter color is 'blue', which represents the color of the column chart is blue.

  1. Multiple color settings
    If you want different columns to use different colors, you can set the parameter 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()
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template