matplotlib学习笔记_其他设置

设置网格grid

grid(color='b', ls = '-.', lw = 0.25)
- color:表示网格线的颜色;
- ls:表示网格线的样式;
- lw:表示网格线的宽度;
#如果您只是想开启不带任何样式的网格,可以通过 grid(True) 来实现。
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()

设置坐标轴格式

#1.设置坐标轴的单位,
#Matplotlib 通过axes对象的xscale或yscale属性来实现对坐标轴的格式设置。
axes[1].set_yscale("log")
#2.设置轴的颜色
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)
#将侧轴、顶部轴设置为None
ax.spines['right'].set_color(None)
ax.spines['top'].set_color(None)
ax.plot([1,2,3,4,5])
plt.show()

设置坐标轴范围

#通过 set_xlim() 和 set_ylim() 对 x、y 轴的数值范围进行设置。
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')
#设置y轴
a1.set_ylim(0,10000)
#设置x轴
a1.set_xlim(0,10)
plt.show()

设置坐标轴刻度

#xticks() 和 yticks() 函数接受一个列表对象作为参数,
#列表中的元素表示对应数轴上要显示的刻度。如下所示:
ax.set_xticks([2,4,6,8,10])
#设置x轴刻度标签
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)
#设置x轴标签
ax.set_xlabel('angle')
ax.set_title('sine')
ax.set_xticks([0,2,4,6])
#设置x轴刻度标签
ax.set_xticklabels(['zero','two','four','six'])
#设置y轴刻度
ax.set_yticks([-1,0,1])
plt.show()

设置中文格式问题

#设置字体
plt.rcParams["font.sans-serif"]=["SimHei"]
#该语句解决图像中的“-”负号的乱码问题
plt.rcParams["axes.unicode_minus"]=False

设置双轴

#Matplotlib 提供的 twinx() 和 twiny() 函数,
#除了可以实现绘制双轴的功能外,还可以使用不同的单位来绘制曲线,
#比如一个轴绘制对函数,另外一个轴绘制指数函数。
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()
#‘ro’表示红色圆点
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'}
#通过执行 imsave() 函数,可以将包含图像数据的 ndarray 数组
#保存到磁盘文件中。
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')
#通过 imshow() 函数可以在 Matplotlib 查看器上绘制出相应的图像。
#其函数格式如下:
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/

posted on   朝朝暮Mu  阅读(167)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示