matplotlib画图笔记(1)

ps:整理了下画图中查到的资料(清空收藏夹)以备下次使用

目录

学习链接

 0.模块导入

1.python matplotlib中axes与axis的区别?

2.matplotlib设置画面大小

3.matplotlib命令与格式:图例legend语法及设置

4.matplotlib三维绘图

5.matplotlib subplot子图

6.matplotlib中添加注解

7.matplotlib绘图设置坐标轴刻度、文本

8.中文显示参数设置

9. matplotlib 如何使x,y轴的单位长度相等呢? 

10.双y轴坐标轴图及控制时间格式

11.matplotlib保存图片


学习链接

https://matplotlib.org/index.html

https://liam.page/2014/09/11/matplotlib-tutorial-zh-cn/

https://blog.csdn.net/qq_31192383/category_6665721.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保存图片

解决使用 plt.savefig 保存图片时一片空白

(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)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fff2zrx

谢谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值