Introduction: Matplotlib is the most basic visualization library in python
1. The composition of the chart
2. Matplotlib chart first experience
#绘制简单的折线图
import matplotib.pyplot as plt
x = [1,2,3,4]#x轴下标
y = [1,2,3,4]#y轴下标
#x,y相对应
plt.plot(x,y)
3. Common settings for charts
Matplotlib mainly uses the plot() function to draw
Matplotlib.pyplot.plot(x,y,format_string,**Kwargs)
main parameters:
x: x-axis data
y: y-axis data
format_string: string that controls the format of the curve, including colors, linestyle, markerstyle
kwargs: key-value pair parameters
3.1. Color setting
The color parameter can control the color of the line.
It can be the color in the table, or it can be a hexadecimal string, an RGB or RGBA tuple in floating point form, such as (0.1, 0.2, 0.5) or (0.1, 0.2, 0.5, 0.3).
#rgb设置颜色
plt.plot(x,y,color=(0.1,0.7,0.5))
#缩写设置颜色
plt.plot(x,y,color='r')
3.2. Line style
The linestyle optional parameter can set the style of the line, "-": solid line, "–": double line, "-.": dotted line, ":": dotted line
plt.plot(x,y,color='r',linestyle='-.')#双画线
3.3. Marker style
marker: optional parameter can set the marker style
mfc: set the color of the marker
mec: set the border color of the marker
plt.plot(x,y,color='r',linestyle='-.',marker='o',mfc='w',mec='c')
3.4. Set the canvas
matpoltib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
num: image number or name, the number is the number, the string is the name, Different canvases can be activated by this parameter.
figsize: Specifies the width and height of the canvas in inches.
dpi: Specifies the resolution of the drawing object, that is, how many pixels per inch, the default value is 80. The larger the pixels, the larger the canvas.
facecolor: background color.
**#########edgecolor: ** Border color.
frameon: Whether to display the frame, the default value is True, the frame is drawn; if it is False, the frame is not drawn.
import matplotlib.pyplot as plt
x = [1,2,3,4]#x轴下标
y = [1,2,3,4]#y轴下标
#设置画布,一定要设置在画图的上边
plt.figure(figsize=(6,3),facecolor='c')
plt.plot(x,y,color=(0.1,0.7,0.5))
plt.plot(x,y,color='r',linestyle='-.',marker='o',mfc='w',mec='c')
3.5. Set the coordinate axis
1. Set the x-axis and y-axis titles mainly using the xlable() and ylable() functions
plt.xlabel('这是x轴')
plt.ylabel('这是y轴')
plt.rcParams['font.sans-serif'] = ['simHei']#解决中文乱码
There will be a problem here: Chinese garbled characters
Solution:
plt.rcParams['font.sans-serif']=['SimHei'] #解决中文乱码
Solve the problem that the negative sign does not display
plt.rcParams['axes.unicode_minus'] = False #解决负号不显示
2. Coordinate axis scale
When drawing a two-dimensional image with matplotlib, the values displayed by the default abscissa (x-axis) and ordinate (y-axis) may sometimes not meet our needs, and we need to use the xticks() function and yticks The () function sets the values of the x-axis and y-axis respectively.
xticks() function syntax:
xticks(locs,[labels],**kwargs)
parameters:
locs: array, indicating the scale on the x-axis
labels: array, the default value is the same as locs, locs indicates the position, and labels determine the position of the position Label
rotation: rotation angle
plt.xticks(range(1,5),[str(x) +'月' for x in range(1,5)])
3. Coordinate axis range
The coordinate axis range refers to the value range of the x-axis and y-axis. To set the coordinate axis range mainly use the xlim() function and ylim() function.
plt.xlim(1,6)
4. Gridlines
plt.grid()