ps:整理了下画图中查到的资料(清空收藏夹)以备下次使用
目录
1.python matplotlib中axes与axis的区别?
3.matplotlib命令与格式:图例legend语法及设置
9. matplotlib 如何使x,y轴的单位长度相等呢?
学习链接
https://matplotlib.org/index.html
0.模块导入
首先对于matplotlib的使用,有两种形式,一种是matplotlib下的模块pyplot,一种是pylab
第一种导入是
from matplotlib import pyplot
import matplotlib as mpl
第二种则直接是
from pylab import *
官方对于的解释是:pylab结合了pyplot和numpy,对交互式使用来说比较方便,既可以画图又可以进行简单的计算。但是,对于一个项目来说,建议分别导入使用
具体可参见这位的笔记
1.python matplotlib中axes与axis的区别?
来自知乎禹洋的回答 - 知乎
用画板和画纸来做比喻的话,figure就好像是画板,是画纸的载体,但是具体画画等操作是在画纸上完成的。在pyplot中,画纸的概念对应的就是Axes/Subplot。
fig = plt.figure()
plt.show()
结果是一个画板
fig = plt.figure()
ax = fig.add_subplot(111)
plt.show()
结果加上了坐标系
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set(xlim=[0.5, 4.5], ylim=[-2, 8],title='An Example Axes', ylabel='Y-Axis',xlabel='X-Axis')
plt.show()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set(xlim=[0.5, 4.5], ylim=[-2, 8],title='An Example Axes', ylabel='Y-Axis',xlabel='X-Axis')
ax.plot([1, 2, 3, 4], [2,5,4,7])
plt.show()
2.matplotlib设置画面大小
fig=plt.figure(figsize=(6, 5))
代表的是画板是600*500的
3.matplotlib命令与格式:图例legend语法及设置
4.matplotlib三维绘图
https://blog.csdn.net/u014636245/article/details/82799573
https://blog.csdn.net/guduruyu/article/details/78050268
5.matplotlib subplot子图
https://blog.csdn.net/claroja/article/details/70841382
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots_adjust.html
6.matplotlib中添加注解
(1)可通过plt.annotate()函数
具体见https://blog.csdn.net/u012735708/article/details/82114565
(2)可通过ax.text()函数
7.matplotlib绘图设置坐标轴刻度、文本
https://www.jb51.net/article/134638.htm
https://blog.csdn.net/u010358304/article/details/78906768
8.中文显示参数设置
https://segmentfault.com/a/1190000005144275
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
9. matplotlib 如何使x,y轴的单位长度相等呢?
https://www.zhihu.com/question/51213258
import pylab as plt
fig=plt.figure()
plt.plot([1,2,3,4])
ax = plt.gca()
#第一种
ax.set_aspect(1)
#第二种
plt.axis("equal")
#第三种
plt.figure(figsize=(6,6)) #figsize=(*,*) 来设置,使这两个值保持相等
10.双y轴坐标轴图及控制时间格式
https://segmentfault.com/a/1190000006158803
11.matplotlib保存图片
(1)在 plt.show()
之前调用 plt.savefig()
import matplotlib.pyplot as plt
""" 一些画图代码 """
plt.savefig("filename.png")
plt.show()
(2)画图的时候获取当前图像(这一点非常类似于 Matlab 的句柄的概念):、
# gcf: Get Current Figure
fig = plt.gcf()
plt.show()
fig1.savefig('tessstttyyy.png', dpi=100)
#! usr/bin/python
#coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
data=np.random.rand(10,10)
fig, ax=plt.subplots()
data[data==-1]=np.nan#去掉缺省值-1
im =ax.imshow(data,interpolation='none',cmap='Reds_r',vmin=0.6,vmax=.9)#不插值
#去掉边框
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
plt.show()
使用 plt.savefig() 输出图片去除旁边的空白区域
subplot_adjust + margin(0,0)