posted on 2023-05-21 18:03 read(631) comment(0) like(13) collect(0)
matplotlib draws histograms, pie charts, scatter plots , bubble charts, box plots, radar charts http://t.csdn.cn/ZeXim
Parameter configuration of matplotlib: http://t.csdn.cn/TiI79
xlabel(xlabel,fontdict=None,labelpad=None)
ylabel(ylabel,fontdict=None,labelpad=None)
xlabel/ylabel: Indicates the label text of the x-axis (y-axis).
fontdict: Represents a dictionary that controls the text style of the label.
labelpad: Indicates the distance between the label and the axis frame (including scale and scale label).
Sample code:
# 标签
import numpy as np
import matplotlib.pyplot as plt
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
y1,y2=np.sin(x),np.cos(x)
plt.plot(x,y1,x,y2)
# 设置标签
plt.xlabel("this is x 轴",size=20)
plt.ylabel("this is y 轴",size=20)
plt.show()
Use the xlim, ylim functions to set the range of the x-axis and y-axis
xlim(left=None,right=None,emit=True,auto=False,xmin=None,xmax=None)
ylim(left=None,right=None,emit=True,auto=False,ymin=None,ymax=None)
# 标签 import numpy as np import matplotlib.pyplot as plt # 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 x=np.linspace(-np.pi,np.pi,256,endpoint=True) y1,y2=np.sin(x),np.cos(x) plt.plot(x,y1,x,y2) # 设置标签 plt.xlabel("this is x 轴",size=20) plt.ylabel("this is y 轴",size=20) # 刻度标签 plt.xlim(-4,4) plt.ylim(-1,1) plt.show()
title(label,fontdict=None,loc="center",pad=None)
Mainly here this loc
legend(handles,labels,loc)
# 标签 import numpy as np import matplotlib.pyplot as plt # 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 x=np.linspace(-np.pi,np.pi,256,endpoint=True) y1,y2=np.sin(x),np.cos(x) pic=plt.plot(x,y1,x,y2) # 设置标签 plt.xlabel("this is x 轴",size=20) plt.ylabel("this is y 轴",size=20) # 刻度标签 plt.xlim(-4,4) plt.ylim(-1,1) # 添加标题 plt.title("正弦曲线和余弦曲线",loc="left") # 添加图例 plt.legend(pic,["sinx","cosx"],shadow=True,fancybox="blue") plt.show()
This is simple, plt.grid()
just fine, but there are still some parameters in it
grid(b=True,axis="both",which='major')
which: display the type of grid, support major, minor, both defaults to major
axis: Indicates which direction to display the grid, there are three options x, y, and both
linewidth or lw: the width of the line
Default :
# 标签 import numpy as np import matplotlib.pyplot as plt # 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 x = np.linspace(-np.pi, np.pi, 256, endpoint=True) y1, y2 = np.sin(x), np.cos(x) pic = plt.plot(x, y1, x, y2) # 设置标签 plt.xlabel("this is x 轴", size=20) plt.ylabel("this is y 轴", size=20) # 添加标题 plt.title("正弦曲线和余弦曲线", loc="left") # 添加图例 plt.legend(pic, ["sinx", "cosx"], shadow=True, fancybox="blue") # 网格 plt.grid() plt.show()
Select the grid to add the y axis, modify lw to 0.3
plt.grid(lw=1, axis="y")
axhline(y=0,xmin=0,xmax-1,linestyle="-")
y: Indicates the coordinates of the horizontal reference line
xmin: Indicates the starting position of the horizontal reference line, the default is 0
xmax: the end position of the horizontal reference line, the default is 1
linstyle: Indicates the type of horizontal reference line, the default is solid line
Other types of linstyle: http://t.csdn.cn/Ufbvt
The syntax of axvline is similar to that of axhline, only need to modify x and y
Example: Adding two reference lines to the image above:
# 标签 import numpy as np import matplotlib.pyplot as plt # 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 x = np.linspace(-np.pi, np.pi, 256, endpoint=True) y1, y2 = np.sin(x), np.cos(x) pic = plt.plot(x, y1, x, y2) # 设置标签 plt.xlabel("this is x 轴", size=20) plt.ylabel("this is y 轴", size=20) # 添加标题 plt.title("正弦曲线和余弦曲线", loc="left") # 添加图例 plt.legend(pic, ["sinx", "cosx"], shadow=True, fancybox="blue") # 网格 plt.grid(lw=1, axis="y") # 参考线 plt.axvline(x=0.5, linestyle=":", color="m") plt.axhline(y=0.5, linestyle="--", color="y") plt.show()
axhspan(ymin,ymax,xmin,xmax)
axvspan(ymin,ymax,xmin,xmax)
# 标签 import numpy as np import matplotlib.pyplot as plt # 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 x = np.linspace(-np.pi, np.pi, 256, endpoint=True) y1, y2 = np.sin(x), np.cos(x) pic = plt.plot(x, y1, x, y2) # 设置标签 plt.xlabel("this is x 轴", size=20) plt.ylabel("this is y 轴", size=20) # 添加标题 plt.title("正弦曲线和余弦曲线") # 添加图例 plt.legend(pic, ["sinx", "cosx"], shadow=True, fancybox="blue") # 网格 plt.grid(lw=1, axis="y") # 参考线 plt.axvline(x=0.5, linestyle=":", color="m") plt.axhline(y=0.5, linestyle="--", color="y") # 参考区域 plt.axvspan(xmin=0.5, xmax=1, alpha=0.32) plt.axhspan(ymin=0.2, ymax=0.5, alpha=0.32) plt.show()
anootate(s,xy,xytext,xycoords,arrowprops,bbox)
The arrowprops parameters are as follows:
text(x,y,s,fontdict=None,bbox)
x, y: Indicates the position of the annotation text.
s: Indicates the content of the annotation text.
fontdict: A dictionary representing the control font.
bbox: Indicates the border attribute bullet of the annotation text
For example:
# 标签 import numpy as np import matplotlib.pyplot as plt # 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 x = np.linspace(-np.pi, np.pi, 256, endpoint=True) y1, y2 = np.sin(x), np.cos(x) pic = plt.plot(x, y1, x, y2) # 设置标签 plt.xlabel("x 轴", size=20) plt.ylabel("y 轴", size=20) # 添加标题 plt.title("正弦曲线和余弦曲线") # 添加图例 plt.legend(pic, ["sinx", "cosx"], shadow=True, fancybox="blue") # 网格 plt.grid() plt.annotate("最小值", xy=(-np.pi / 2, -1.0), xytext=((-np.pi / 2), -0.5), arrowprops=dict(arrowstyle="->")) plt.text(3.1, 0.1, "y=sin(x)", bbox=dict(alpha=0.2)) plt.show()
Just use latetx or katex syntax
For example, r"$\sin x$"
it will appear as
sin
x
\sin x
sinx
Modify the title here
# 标签 import numpy as np import matplotlib.pyplot as plt # 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 x = np.linspace(-np.pi, np.pi, 256, endpoint=True) y1, y2 = np.sin(x), np.cos(x) pic = plt.plot(x, y1, x, y2) # 设置标签 plt.xlabel(" x 轴", size=15) plt.ylabel(" y 轴", size=15) # 添加标题 plt.title(r'$\frac{\sin x}{2}$') # 添加图例 plt.legend(pic, ["sinx", "cosx"], shadow=True, fancybox="blue") # 网格 plt.grid() plt.annotate("最小值", xy=(-np.pi / 2, -1.0), xytext=((-np.pi / 2), -0.5), arrowprops=dict(arrowstyle="->")) plt.text(3.1, 0.1, "y=sin(x)", bbox=dict(alpha=0.2)) plt.show()
Reference link: Matplotlib — Visualization with Python
Reference Book: Python Data Visualization%20 (Dark Horse Programmer)
Author:Sweethess
link:http://www.pythonblackhole.com/blog/article/25336/bc765e0fc8d84026be03/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!