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

How to set color for column chart in Matplotlib library

小老鼠
Release: 2023-12-20 17:50:17
Original
1356 people have browsed it

Setting methods include using the bar() function, using the color parameter of the bar() function, and using the edgecolor parameter of the bar() function. Detailed introduction: 1. Use the bar() function, the color parameter is used to specify the color of the column, which can be a string of color names or a hexadecimal color code; 2. Use the color parameter of the bar() function, the color parameter is a A list containing colors, each element in the list corresponds to the color of a column; 3. Use the edgecolor parameter of the bar() function.

How to set color for column chart in Matplotlib library

The operating system for this tutorial: Windows 10 system, Python version 3.11.4, Dell G3 computer.

In the Matplotlib library, you can use the following methods to set the color of the column chart:

1. Use the bar() function to set the color:

python

import matplotlib.pyplot as plt  
  
# 创建数据  
x = [1, 2, 3, 4, 5]  
y = [10, 20, 30, 40, 50]  
  
# 创建柱形图并设置颜色  
plt.bar(x, y, color='blue')  
  
# 显示图形  
plt.show()
Copy after login

In the above code, the color parameter is used to specify the color of the column, which can be a string of color names (such as 'blue', 'red', etc.) or a hexadecimal color code (such as '#FF0000' for red) .

2. Use the color parameter of the bar() function to set the color:

python

import matplotlib.pyplot as plt  
  
# 创建数据  
x = [1, 2, 3, 4, 5]  
y = [10, 20, 30, 40, 50]  
colors = ['blue', 'green', 'red', 'purple', 'orange']  
  
# 创建柱形图并设置颜色  
plt.bar(x, y, color=colors)  
  
# 显示图形  
plt.show()
Copy after login

In the above code, the color parameter is a list containing colors. Each element corresponds to the color of a column.

3. Use the edgecolor parameter of the bar() function to set the border color:

python

import matplotlib.pyplot as plt  
  
# 创建数据  
x = [1, 2, 3, 4, 5]  
y = [10, 20, 30, 40, 50]  
colors = ['blue', 'green', 'red', 'purple', 'orange']  
  
# 创建柱形图并设置颜色和边框颜色  
plt.bar(x, y, color=colors, edgecolor='black')  
  
# 显示图形  
plt.show()
Copy after login

In the above code, the edgecolor parameter is used to specify the color of the column border.

The above is the detailed content of How to set color for column chart 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