How to use the plot function in Python

小老鼠
Release: 2023-11-13 15:22:37
Original
2237 people have browsed it

In Python, the plot function is a function in the Matplotlib library, used to draw graphics of data.

The basic usage of the plot function is as follows:

import matplotlib.pyplot as plt # 准备数据 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 绘制折线图 plt.plot(x, y) # 显示图形 plt.show()
Copy after login

The above code is first imported matplotlib.pyplot module and creates a set of data x and y. Then use the plot function to plot the data into a line chart. Finally, call the show function to display the graph.

You can also customize the plot's style, labels, etc. by passing additional parameters. The following are some commonly used parameters:

linestyle: Specify the style of the line, such as '-' for solid line, '--' for dashed line, ':' for dotted line, etc.

color: Specify the color of the line, you can use color names (such as 'red', 'blue') or color codes (such as '#FF0000' for red).

marker: Specify the mark style of the data point, such as 'o' for a circle, 's' for a square, ' ' for a plus sign, etc.

label: Specify the label of the legend, used to identify different data series.

xlabel and ylabel: Specify the labels for the x-axis and y-axis.

title: Specify the title of the graphic.

The following is an example that demonstrates how to use the plot function to draw a line chart with a custom style and legend:

import matplotlib.pyplot as plt # 准备数据 x = [1, 2, 3, 4, 5] y1 = [2, 4, 6, 8, 10] y2 = [1, 3, 5, 7, 9] # 绘制折线图 plt.plot(x, y1, linestyle='--', color='red', marker='o', label='Line 1') plt.plot(x, y2, linestyle='-', color='blue', marker='s', label='Line 2') # 添加图例、标签和标题 plt.legend() plt.xlabel('X') plt.ylabel('Y') plt.title('Plot Example') # 显示图形 plt.show()
Copy after login

In the above example, the plot function is used to draw two line charts, and the style and legend are customized through the linestyle, color, marker, and label parameters. Finally, the legend, label, and title were added using the legend, xlabel, ylabel, and title functions.

The above is the detailed content of How to use the plot function in Python. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!