Article directory
introduce
plt.plot()
Is one of the main functions in the Matplotlib library for drawing line graphs (line graphs). Its role is to connect a set of data points to visualize trends, relationships, or patterns in the data. The following is plt.plot()
a detailed introduction:
plt.plot(x, y, fmt, **kwargs)
x
: Represents the data points on the X-axis, usually a list, array or one-dimensional sequence, used to specify the horizontal position of the data points.y
: Represents the data points on the Y-axis, usually also a list, array or one-dimensional sequence, used to specify the vertical position of the data points.fmt
: is an optional format string that specifies the style, label, and color of the line. For example, 'ro-' represents a red dotted line.**kwargs
: is a series of optional parameters used to further customize the properties of the line, such as line width, mark size, label, etc.
The following are some common parameters and usage:
-
Style parameters (fmt): The format string can contain one character to specify the color, one character to specify the mark style, and one character to specify the line style. For example, 'r-' represents a solid red line, and 'bo–' represents a dashed blue dotted line.
-
Line style (linestyle): Use
linestyle
parameters to specify the style of the line, such as solid line ('-'), dashed line ('–'), dot-dash line ('-.'), etc. -
Marker style (marker): Use
marker
parameters to specify the mark style of data points, such as dots ('o'), squares ('s'), asterisks ('*'), etc. -
Line color (color): Use
color
parameters to specify the color of the line. You can use a color name (such as 'red'), abbreviation (such as 'r') or hexadecimal color code (such as '#FF5733'). -
Linewidth: Use
linewidth
parameters to specify the width of the line, expressed as a number. -
Marker size (markersize): Use
markersize
parameters to specify the size of the marker, expressed as a number. -
Legend label (label): Use
label
parameters to specify labels for lines, which are used to create legends. -
Other properties: There are many other properties available for customizing line drawings, such as transparency, gradient, line style, shading, etc.
plt.plot()
Not only can it draw simple line graphs, it can also be used to draw multiple lines, add legends, labels, titles, set axis ranges and scales, etc. It is one of the most commonly used plotting functions in Matplotlib and is suitable for visualizing trends and relationships in data sets.
Code example
import matplotlib.pyplot as plt
#显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 示例数据
x = [1, 2, 3, 4, 5]
y = [10, 15, 13, 18, 16]
# 绘制线图,并自定义外观
plt.plot(
x, # X轴数据
y, # Y轴数据
marker='o', # 标记样式:圆点
linestyle='-', # 线条样式:实线
color='green', # 线条颜色:蓝色
linewidth=2, # 线宽:2
markersize=10, # 标记大小:8
label='数据1' # 图例标签
)
# 添加标签和标题
plt.xlabel('X轴标签')
plt.ylabel('Y轴标签')
plt.title('标题')
# 添加图例
plt.legend()
# 显示网格线
plt.grid(True)
# 自定义刻度
plt.xticks([1, 2, 3, 4, 5], ['一', '二', '三', '四', '五'])
# 显示图表
plt.show()