matplotlib画图总结--常用功能
0、内容范围
多曲线图、图例、坐标轴、注释文字等。
1、曲线图
多曲线图、图例、网格、坐标轴名称、图标名、坐标轴范围等。
from matplotlib import pyplot as plt
import numpy as np x = np.linspace(-np.pi, np.pi, 200, endpoint=True)
c, s = np.cos(x), np.sin(x)
plt.xlim(-np.pi, np.pi)
# p1 = plt.plot(x,c,'r', label = 'cos')
# p2 = plt.plot(x,s,'b', label = 'sin')
p1 = plt.plot(x,c,'r')
p2 = plt.plot(x,s,'b')
plt.xlabel('x')
plt.ylabel('y')
plt.title('cos and sin')
plt.legend( ['cos', 'sin'])
plt.grid(True)
plt.show()

知识点:
1)一个图里绘制条曲线
方法1:
plt.plot(x,c,'r', x,s,'b')
方法2:
p1 = plt.plot(x,c,'r')
p2 = plt.plot(x,s,'b')
2)给图添加图例
方法1:
p1 = plt.plot(x,c,'r', label = 'cos')
p2 = plt.plot(x,s,'b', label = 'sin')
plt.legend()
方法2:
p1 = plt.plot(x,c,'r')
p2 = plt.plot(x,s,'b')
plt.legend( ['cos', 'sin'])
推荐使用方法1,因为方法2必须注意添加曲线的顺序。
2、柱状图
学习柱状图、为图形添加字符坐标轴.
import numpy as np
import matplotlib.pyplot as plt N = 5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence p1 = plt.bar(ind, menMeans, width, yerr=menStd, label='men')
p2 = plt.bar(ind, womenMeans, width, bottom=menMeans, yerr=womenStd, label='women') plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend(loc='upper right')
plt.show()

为什么会两个图层叠?
p1 = plt.bar(ind, menMeans, width, yerr=menStd, label='men')
p2 = plt.bar(ind, womenMeans, width, bottom=menMeans, yerr=womenStd, label='women')
bar函数的x坐标一样,并且y坐标值是bottom=menMeans,所以第二个图会堆叠在第一个图上面。
下面实现2条柱状图。
import matplotlib
import matplotlib.pyplot as plt
import numpy as np labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25] x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women') # Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend() plt.show()

x轴的坐标不是数子,实现方法如下,先设定坐标轴的tick,然后把ticklabel改为字符值。
ax.set_xticks(x)
ax.set_xticklabels(labels)
如果需要实现水平柱状图,则使用ax.barh(y_pos, performance, xerr=error, align='center')函数。
3、添加文字、注释、箭头指示、花式文字
其中,text 函数可以做文本注释,且支持 LaTeX 格式,可以在图例中写公式。
text(x,y,string,fontsize=15,verticalalignment="top",horizontalalignment="right")
x = np.arange(0, 10, 0.1)
plt.plot(x, x**2)
plt.grid(True) # 设置网格线
plt.text(5,50, "TEXT1")
plt.show()

花式文本:
You can put a rectangular box around the text instance (e.g., to set a background color) by using the keyword bbox. bbox is a dictionary of Rectangle properties. For example:
>>> text(x, y, s, bbox=dict(facecolor='red', alpha=0.5))
import matplotlib.pyplot as plt
import numpy as np plt.text(0.6, 0.5, "Text", size=50, rotation=45, ha="center", va="center", color='b')
plt.text(0.3, 0.5, "Text", size=25, rotation=10, ha="center", va="center", bbox=dict(boxstyle="round",ec=(1, 0.5, 0.5),fc=(1., 0.8, 0.8),)) #颜色ec、fc plt.plot()
plt.show()

添加箭头和说明信息
plt.annotate('value of 5**2', xy=(5, 25), xytext=(6, 26), arrowprops=dict(facecolor='black', shrink=0.05))

matplotlib画图总结--常用功能的更多相关文章
- matplotlib 画图
matplotlib 画图 1. 画曲线图 Tompson = np.array([0, 0, 0, 0, 0.011, 0.051, 0.15, 0.251, 0.35, 0.44, 0 ...
- WebStorm 常用功能的使用技巧分享
WebStorm 是 JetBrain 公司开发的一款 JavaScript IDE,使用非常方便,可以使编写代码过程更加流畅. 本文在这里分享一些常用功能的使用技巧,希望能帮助大家更好的使用这款强大 ...
- AVA正则表达式4种常用功能
正则表达式在字符串处理上有着强大的功能,sun在jdk1.4加入了对它的支持 下面简单的说下它的4种常用功能: 查询: String str="abc efg ABC"; Str ...
- [转]WebPack 常用功能介绍
概述 Webpack是一款用户打包前端模块的工具.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑.打包其他的静态资源,包括css.image.font file.templa ...
- FastReport.Net 常用功能总汇
一.常用控件 文本框:输入文字或表达式 表格:设置表格的行列数,输入数字或表达式 子报表:放置子报表后,系统会自动增加一个页面,你可以在此页面上设计需要的报表.系统在打印处理时,先按主报表打印,当碰到 ...
- python3 字符串与列表常用功能
一.字符串常用功能 1. capitalize(),将字符串的首字母变成大写,其余全部置为小写:如果字符串中有多个单词,也只是将第一个单词的首字母置为大写:例: >>> name = ...
- matlab进阶:常用功能的实现,常用函数的说明
常用功能的实现 获取当前脚本所在目录 current_script_dir = fileparts(mfilename('fullpath')); % 结尾不带'/' 常用函数的说明 bsxfun m ...
- WebPack常用功能介绍
概述 Webpack是一款用户打包前端模块的工具.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑.打包其他的静态资源,包括css.image.font file.templa ...
- JavaScript 常用功能总结
小编吐血整理加上翻译,太辛苦了~求赞! 本文主要总结了JavaScript 常用功能总结,如一些常用的JS 对象,基本数据结构,功能函数等,还有一些常用的设计模式. 目录: 众所周知,JavaScri ...
随机推荐
- 最新JetBrainsPyCharm自动部署Python(Django,tornado等)项目至远程服务器
每次开发Python项目时,对于所有Python开发人员来说,最枯燥的不是修改代码,而是实时将自己的代码上传至远程服务器,进行测试或者部署,本人最初开发也是这样,通过使用Xshell 5,WinSCP ...
- javascript中判断数据类型
编写javascript代码的时候常常要判断变量,字面量的类型,可以用typeof,instanceof,Array.isArray(),等方法,究竟哪一种最方便,最实用,最省心呢?本问探讨这个问题. ...
- phpexcel导出数字带E的解决方法
phpexcel导出数字带E的解决方法 excel之所以带E 是因为按照数字格式来显示了(数字过长的时候) 数字左边或者右边加空格就变成字符串了 那么excel就会按照字符串格式来显示了 就不会带E了
- Kubernetes(k8s)集群安装
一:简介 二:基础环境安装 1.系统环境 os Role ip Memory Centos 7 master01 192.168.25.30 4G Centos 7 node01 192.168.25 ...
- linux搭建简易网站
1.检查环境 getenforce #查看seLinux运行状态 Enforcing #强行执行 setenforce #临时关闭selinux vim /etc/selinux/config #编辑 ...
- T-SQL Part IV: ORDER BY
ORDER BY 返回一个Cursor,并不返回结果集.而试图将Cursor作为输入将产生了错误. 所以,下列的SQL语句将产生错误: SELECT VerID, IsComplete VerID, ...
- hdu 3549 Flow Problem (Dinic)
Flow ProblemTime Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- 反汇编分析objc函数枢纽objc_msgSend
在分析objc_msgSend之前,先来搞清楚另一个问题. 函数是什么?可能会答 void foo(void) {} 像这样就是一个函数.或者函数包括函数原型和函数定义,是一段执行某样功能的机器代码. ...
- ecryptfs
ecryptfs是一种加密文件系统.该文件系统的内容在传输和储存时以密文形式存在.只有在mount时用密钥解密才能得到明文.利用这个特性,我们可以用他来对软件镜像中的部分敏感文件系统进行加密,然后打包 ...
- GDG Xi'an DevFest 2019 闪电演讲 -《假如我是一个浏览器》PPT(经典多图,建议收藏)
GDG Xi'an DevFest2019演讲PPT链接: http://tmp.link/f/5dd9e6bf461b6 闪电演讲<假如我是一个浏览器>PPT链接: https://gi ...