设置网格grid
| grid(color='b', ls = '-.', lw = 0.25) |
| - color:表示网格线的颜色; |
| - ls:表示网格线的样式; |
| - lw:表示网格线的宽度; |
| |
| import matplotlib.pyplot as plt |
| import math |
| import numpy as np |
| x = np.arange(1,math.pi*2,0.05) |
| fig,ax = plt.subplots(1,3,figsize=(12,4)) |
| ax[0].plot(x,np.sin(x)) |
| ax[0].grid(True) |
| plt.show() |
设置坐标轴格式
| |
| |
| axes[1].set_yscale("log") |
| |
| import matplotlib.pyplot as plt |
| fig = plt.figure() |
| ax = fig.add_axes([0,0,1,1]) |
| |
| ax.spines['bottom'].set_color('blue') |
| ax.spines['left'].set_color('red') |
| ax.spines['left'].set_linewidth(2) |
| |
| ax.spines['right'].set_color(None) |
| ax.spines['top'].set_color(None) |
| ax.plot([1,2,3,4,5]) |
| plt.show() |
设置坐标轴范围
| |
| import matplotlib.pyplot as plt |
| fig = plt.figure() |
| a1 = fig.add_axes([0,0,1,1]) |
| import numpy as np |
| x = np.arange(1,10) |
| a1.plot(x, np.exp(x),'r') |
| a1.set_title('exp') |
| |
| a1.set_ylim(0,10000) |
| |
| a1.set_xlim(0,10) |
| plt.show() |
设置坐标轴刻度
| |
| |
| ax.set_xticks([2,4,6,8,10]) |
| |
| ax.set_xticklabels(['zero','two','four','six']) |
| |
| import matplotlib.pyplot as plt |
| import numpy as np |
| import math |
| x = np.arange(0, math.pi*2, 0.05) |
| |
| fig = plt.figure() |
| |
| ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) |
| y = np.sin(x) |
| ax.plot(x, y) |
| |
| ax.set_xlabel('angle') |
| ax.set_title('sine') |
| ax.set_xticks([0,2,4,6]) |
| |
| ax.set_xticklabels(['zero','two','four','six']) |
| |
| ax.set_yticks([-1,0,1]) |
| plt.show() |
设置中文格式问题
| |
| plt.rcParams["font.sans-serif"]=["SimHei"] |
| |
| plt.rcParams["axes.unicode_minus"]=False |
设置双轴
| |
| |
| |
| |
| import matplotlib.pyplot as plt |
| import numpy as np |
| |
| fig = plt.figure() |
| |
| a1 = fig.add_axes([0,0,1,1]) |
| |
| x = np.arange(1,11) |
| |
| a1.plot(x,np.exp(x)) |
| a1.set_ylabel('exp') |
| |
| a2 = a1.twinx() |
| |
| a2.plot(x, np.log(x),'ro-') |
| |
| a2.set_ylabel('log') |
| |
| fig.legend(labels = ('exp','log'),loc='upper left') |
| plt.show() |
保存图像
参考:https://blog.csdn.net/weixin_42279212/article/details/120665026
| fig.savefig("first.png") |
| |
| {'ps': 'Postscript', |
| 'eps': 'Encapsulated Postscript', |
| 'pdf': 'Portable Document Format', |
| 'pgf': 'PGF code for LaTeX', |
| 'png': 'Portable Network Graphics', |
| 'raw': 'Raw RGBA bitmap', |
| 'rgba': 'Raw RGBA bitmap', |
| 'svg': 'Scalable Vector Graphics', |
| 'svgz': 'Scalable Vector Graphics', |
| 'jpg': 'Joint Photographic Experts Group', |
| 'jpeg': 'Joint Photographic Experts Group', |
| 'tif': 'Tagged Image File Format', |
| 'tiff': 'Tagged Image File Format'} |
| |
| |
| import matplotlib.pyplot as plt |
| import matplotlib.image as mpimg |
| import numpy as np |
| img = mpimg.imread('mtplogo.png') |
| plt.imsave("logo.png", img, cmap = 'gray', origin = 'lower') |
| |
| |
| imgplot = plt.imshow(img) |
设置图例 legend
参考1:https://blog.51cto.com/u_15485092/5032951
| ax.legend(handles, labels, loc) |
参考2:https://blog.csdn.net/sinat_39620217/article/details/123757881
绘制曲线
参考:https://blog.csdn.net/SL_World/article/details/120789907
设置颜色
参考:https://xkcd.com/color/rgb/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人