How to customize the color of column chart in Matplotlib library

WBOY
Release: 2024-01-17 09:22:06
Original
1513 people have browsed it

How to customize the color of column chart in Matplotlib library

Matplotlib is one of the commonly used data visualization libraries in Python, which provides rich drawing functions. When drawing a column chart, we can increase the readability and beauty of the chart by changing the color. The following will introduce in detail how to use the Matplotlib library to change the color of the column chart, and provide specific code examples.

In Matplotlib, we can use the bar function to draw a bar chart. The basic usage of this function is as follows:

plt.bar(x, height, width, color)
Copy after login

Among them, x represents the x coordinate of the column chart, height represents the height of the column, width Indicates the width of the column, color indicates the color of the column.

Next we will introduce two commonly used methods to change the color of the column chart.

Method 1: Use color names or codes

The Matplotlib library supports using predefined color names or codes to set the color of the column chart. The following are some commonly used color names and codes:

  • Color names: 'red', 'blue', 'green', 'yellow', 'orange', 'purple', 'gray', 'black', 'white'
  • Color code: 'r', 'b', 'g', 'y', 'm', 'c', 'k', 'w'

We can directly pass the color name or code as a parameter to color, as shown below:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 8, 5, 7, 6]

plt.bar(x, y, color='blue')
plt.show()
Copy after login

In the above example, we set the color of the column chart is blue.

Method 2: Use color mapping

In addition to using predefined color names or codes, Matplotlib also supports using color mapping to set the color of the column chart. Color mapping is a way of mapping data to colors, which can be used to better demonstrate changes in the data. Matplotlib provides the cm module to support common color mappings.

Here is an example using color mapping:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

x = [1, 2, 3, 4, 5]
y = [10, 8, 5, 7, 6]

colors = cm.Reds(np.linspace(0, 1, len(x)))

plt.bar(x, y, color=colors)
plt.show()
Copy after login

In the above example, we use cm.Reds to map the data into the colors of the red series and pass np.linspaceSpecifies the range of color mapping.

Through the above two methods, we can easily change the color of the column chart and increase the readability and beauty of the chart.

To sum up, this article introduces how to change the color of the column chart in the Matplotlib library and gives specific code examples. By using predefined color names or codes and color mapping, we can flexibly set the color of the column chart according to our needs to achieve better data visualization.

The above is the detailed content of How to customize the color of column chart in Matplotlib library. For more information, please follow other related articles on the PHP Chinese website!

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