Matplotlib画图常用方法总结(全)
Matplotlib画图指南
常见的python的matplotlib 的画图的方法,画散点图,曲线图,子图,以及子图的布局,间距等。
1:首先导入包,因为画图常用的数据类型我们需要导入包numpy
import numpy as np
import matplotlib.pyplot as plt
2:创建新图
fig=plt.figure()
Fig=plt.figure(1)
使用add_subplot创建多个子图 subplot
Ax1=fig.add_subpot(2,2,1)
新图和子图之间的关系:
3:使用plot()函数画图
plot()画曲线图,可设置点的格式,线的格式。
Scatter()划散点图,可以设置散点的样式。
matplotlib.pyplot.scatter(x, y,s=20, c=None, marker='o', cmap=None, norm=None, vmin=None, vmax=None,alpha=None, linewidths=None, verts=None, edgecolors=None,hold=None, data=None, **kwargs
)
基本的x ,y参数,还有c,s,alpha和marker,c就是为点指定的颜色数组,s是点的面积大小,alpha是点的颜色的透明度,marker是指定点标记的形状,linewidths
为线宽
。在例子里指定透明度为0.5,c表示颜色常见的颜色为 ['r','y','g','b','r','y','g','b','r'] ,
S[1,2,3,4,5…100]表示形状的面积。我们要改变的是marker的值,marker有很多值可供选择,下表展示了在例子代码的基础上,改变marker的值后的效果:
· 使用HTML十六进制字符串 color='eeefff' 使用合法的HTML颜色名字(’red’,’chartreuse’等)。
· 也可以传入一个归一化到[0,1]的RGB元祖。 color=(0.3,0.3,0.4)
用来该表线条的属性
线条风格linestyle或ls |
描述 |
线条风格linestyle或ls |
描述 |
|
‘-‘ |
实线 |
‘:’ |
虚线 |
|
‘–’ |
破折线 |
‘None’,’ ‘,’’ |
什么都不画 |
|
‘-.’ |
联合使用‘颜色标记线条属性’'bs-.','go-'
plt.plot(t,t, 'r--', t, t**2, 'bs', t, t**3, 'g^'),叠加图
plot(X,S, color="red", linewidth=2.5, linestyle="-", label="sine")
plot(X,C, color="blue", linewidth=2.5, linestyle='-', label='cosine')
简写版:
plt.plot([1,2,3],[1,2,3], 'go-', label='line 1', linewidth=2)
plt.scatter(x,y, s=area, c=colors, alpha=0.5, marker=(9, 3, 30))
也可以不断地修改颜色和值的大小和形状样式
5:细化横轴和纵轴的设置,设置横轴、纵轴的界限以及标注
plt.xlim(X.min(),X.max())
plt.ylim(y.min(),y.max())
或者
plt.axis([X.min(),X.max(),y.min(), y.max()])
6:设置轴记号
plt.xticks()/plt.yticks()设置轴记号
例如:xticks([-np.pi, -np.pi/2, 0, np.pi/2,np.pi],
[r'$-\pi$',r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
yticks([-1,0, +1],[r'$-1$', r'$0$', r'$+1$'])
有plt.xticks([1,2,3,…])会根据设置来显示标记
没有plt.xticks([1,2,3,…])会根据具体情况来自动设置标记(比较好,省事)。
7:plt.legend()添加图例
Legend(loc):使用loc参数
plt.legend(loc='lower left')
0: ‘best' 1: ‘upper right' 2: ‘upper left' 3: ‘lower left' |
4: ‘lower right' 5: ‘right' 6: ‘center left' |
7: ‘center right' 8: ‘lower center' 9: ‘upper center' 10: ‘center' |
plt.legend(loc='best',frameon=False) #去掉图例边框,推荐使用,效果比较美观。
plt.legend(loc='best',edgecolor='blue') #设置图例边框颜色
plt.legend(loc='best',facecolor='blue') #设置图例背景颜色,若无边框,参数无效
用法1:
plot(X, C, color="blue", linewidth=2.5,linestyle="-", label="cosine")
plot(X, S, color="red", linewidth=2.5,linestyle="-", label="sine")
legend(loc='upper left')
用法2:plt.plot(x, y, color="red",linewidth=2.5, linestyle="-")
Plt.legend('图例名',loc='best',frameon=False)
用法3;显示多个图例
p1, = plt.plot([1,2,3])
p2, = plt.plot([3,2,1])
l1 = plt.legend([p2, p1], ["line 2", "line1"], loc='upper left')
8:添加文字说明
· text()可以在图中的任意位置添加文字,并支持LaTex语法
· xlable(), ylable()用于添加x轴和y轴标签
· title()用于添加图的题目
plt.xlabel('Smarts') plt.ylabel('Probability')
#添加标题 plt.title('Histogram of IQ')
#添加文字 plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
9:让图片能够正常显示负数和中文:
import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
mpl.rcParams['axes.unicode_minus']=False#用来正常显示负号
plt.plot(a,'b.',markersize=10, label=u'真'),
中文使用之前加u 如data[u’身高’]
附加的设置:
1:plt.figure(figsize=(12,16)),设置图片的大小
2:调整子图的距离。
plt.tight_layout()#调整每隔子图之间的距离。自动调整,有时候效果也不太好。
调整子图之间的边距;
plt.subplots_adjust(left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None)
left = 0.125 # the left side of thesubplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = 0.2 # the amount of width reserved for blank space between subplots,
# expressed as a fraction of the average axis width
hspace = 0.2 # the amount of height reserved for white space between subplots,
# expressed as a fraction of the average axis height
3:子图的布局
首先按照(2,2)划分,第一个图为(2,2,1)和(2,2,2)
然后把下面两个图合成一个,则上面两个图为(2,1,2)
最后:举一个栗子
import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
mpl.rcParams['axes.unicode_minus']=False #用来正常显示负号
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1,10)
y = x
fig = plt.figure(1)
ax1 = fig.add_subplot(211)
ax1.set_title('图一的第一张子图')
plt.xlabel('X1')
plt.ylabel('Y1')
sValue = x*10
ax1.scatter(x,y,s=sValue,c='r',marker='1')
plt.axis([0, 10,0,10])
ax1.legend('图例1.1')
ax2 = fig.add_subplot(212)
ax2.set_title('图一的第二张子图')
plt.xlabel('X12')
plt.ylabel('Y12')
plt.plot(x, y, 'go-')
plt.axis([0, 10,0,10])
ax2.legend('图例1.2')
x = np.arange(1,100)
y = x
fig = plt.figure(2)
ax1 = fig.add_subplot(211)
ax1.set_title('图2的第一张子图')
plt.xlabel('X1')
plt.ylabel('Y1')
sValue = x*10
colo=['r','y','g','b','r','y','g','b','r','m']
ax1.scatter(x,y,s=sValue ,c=colo)
plt.axis([0, 10,0,10])
ax1.legend('图例1.1')
ax2 = fig.add_subplot(212)
#设置标题
ax2.set_title('图二的第二张子图')
#设置X轴标签
plt.xlabel('X12')
#设置Y轴标签
plt.ylabel('Y12')
#画折线图
plt.plot(x, y, color="red", linewidth=2.5, linestyle="-")
plt.axis([0, 100,0,100])
#设置图标
ax2.legend('图例1.2',loc='best',frameon=False)
plt.show()
Matplotlib画图常用方法总结(全)相关推荐
- Matplotlib.pyplot 常用方法
Matplotlib.pyplot 常用方法 1.介绍 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.通过 Matplotl ...
- matplotlib画图、如何提高图像分辨率?
matplotlib画图.如何提高图像分辨率? Figure size (figsize) determines the size of the figure in inches. This give ...
- Python-anaconda-Spyder使用matplotlib画图无法显示报错解决:Figures now render in the Plots pane by default. To mak
晚上在用anaconda的Spyder IDE,用 matplotlib 画图时不会显示图片在iPython终端中,报错如下: Figures now render in the Plots pane ...
- python matplotlib画图的几个实例--latex,坐标系等
文章目录 实例1 学会使用tex/latex 实例2 学会画坐标轴 2.1过程 2.2 典型例子 2.2.1 一条带箭头的竖线 2.2.2 坐标系 2.2.3 坐标系上画三角函数 实例1 学会使用te ...
- python画图中文显示_解决Linux系统中python matplotlib画图的中文显示问题
最近想学习一些python数据分析的内容,就弄了个爬虫爬取了一些数据,并打算用Anaconda一套的工具(pandas, numpy, scipy, matplotlib, jupyter)等进行一些 ...
- python matplotlib画图产生的Type 3 fonts字体没有嵌入问题
ScholarOne's 对python matplotlib画图产生的Type 3 fonts字体不兼容,更改措施: 在程序中添加如下语句 import matplotlib matplotlib. ...
- python自定义colorbar_python可视化 matplotlib画图使用colorbar工具自定义颜色
python matplotlib画图使用colorbar工具自定义颜色 colorbar(draw colorbar without any mapple/plot) 自定义colorbar可以画出 ...
- 使用matplotlib画图时不能同时打开太多张图
使用matplotlib画图时有时会收到来自matplotlib的runtime warming的警告,原因可能是同时打开太多张图,最常见的情况是在一个循环中画图,每次循环都新建一个图,但是未关闭新建 ...
- Matplotlib画图教程:在QT界面中嵌入三维图片
Matplotlib画图教程:在QT界面中嵌入三维图片 需求: 做项目报告的时候,有这么一个想法,就是能通过UI随时调用matplotlib进行二维图和三维图的绘制.因此就诞生了做这么一个小模块的想法 ...
最新文章
- c语言顺序表有效元素长度,用C语言描述的顺序表类型
- BZOJ 1589 Trick or Treat on the Farm (tarjan缩点,记忆化搜索)[Usaco 2008 Dec Gold]【BZOJ计划】
- Java axis 配置host_Java AxisProperties类代码示例
- 开源wkhtmltopdf使用心得 (四)
- css 识别变量中的换行符_Python编程 第二章——变量和简单数据类型
- 【Python】Pandas中的宝藏函数-apply
- 关于你,关于我. 你好 5G
- server 2008 服务器不能访问 java项目,Java 8上的SQL Server JDBC错误:驱动程序无法使用安全套接字层(SSL)加密建立到SQL Server的安全连接...
- Java-Type简单分类
- 基于FPGA实现PCIE IP功能仿真
- Spring(十九)之异常处理
- UiPath Excel 向下填充
- CPU的使用率和负载的区别
- springboot 自定义webroot的目录
- matlab badsubscript,matlab错误:Subscript indices must either be real positive integers or logicals....
- windows屏幕亮度调节失灵的解决方法
- 英语之脆弱的,易受伤的
- 项目二任务六 任务七 任务八
- 阿里云服务器安装云助手客户端
- 戴尔 服务器重装后蓝屏,dell笔记本不能重装系统,一直进入蓝屏界面?
热门文章
- Codeup-问题 A: 最长公共子序列
- hdu4004 The Frog's Games 二分
- [leetcode] 746.使用最小花费爬楼梯
- 计算机网络实验(华为eNSP模拟器)——第二章 VRP通用路由平台介绍
- linux 命令 cd -p,Linux_实例讲解Linux中cd命令切换目录的使用技巧,cd命令大家再熟悉不过了,bash sh - phpStudy...
- git stash 强制恢复_git操作与分支管理规范
- Python爬虫 解析库的使用
- 单片机编程php,STC单片机内部FLASH读写程序(最新整理)
- ubuntu下gcc的安装与使用
- linux nfs 修复文件,linux nfs Read-only file system