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画图总结--常用功能的更多相关文章

  1. matplotlib 画图

    matplotlib 画图 1. 画曲线图       Tompson = np.array([0, 0, 0, 0, 0.011, 0.051, 0.15, 0.251, 0.35, 0.44, 0 ...

  2. WebStorm 常用功能的使用技巧分享

    WebStorm 是 JetBrain 公司开发的一款 JavaScript IDE,使用非常方便,可以使编写代码过程更加流畅. 本文在这里分享一些常用功能的使用技巧,希望能帮助大家更好的使用这款强大 ...

  3. AVA正则表达式4种常用功能

    正则表达式在字符串处理上有着强大的功能,sun在jdk1.4加入了对它的支持 下面简单的说下它的4种常用功能: 查询: String str="abc efg ABC";  Str ...

  4. [转]WebPack 常用功能介绍

    概述 Webpack是一款用户打包前端模块的工具.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑.打包其他的静态资源,包括css.image.font file.templa ...

  5. FastReport.Net 常用功能总汇

    一.常用控件 文本框:输入文字或表达式 表格:设置表格的行列数,输入数字或表达式 子报表:放置子报表后,系统会自动增加一个页面,你可以在此页面上设计需要的报表.系统在打印处理时,先按主报表打印,当碰到 ...

  6. python3 字符串与列表常用功能

    一.字符串常用功能 1. capitalize(),将字符串的首字母变成大写,其余全部置为小写:如果字符串中有多个单词,也只是将第一个单词的首字母置为大写:例: >>> name = ...

  7. matlab进阶:常用功能的实现,常用函数的说明

    常用功能的实现 获取当前脚本所在目录 current_script_dir = fileparts(mfilename('fullpath')); % 结尾不带'/' 常用函数的说明 bsxfun m ...

  8. WebPack常用功能介绍

    概述 Webpack是一款用户打包前端模块的工具.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑.打包其他的静态资源,包括css.image.font file.templa ...

  9. JavaScript 常用功能总结

    小编吐血整理加上翻译,太辛苦了~求赞! 本文主要总结了JavaScript 常用功能总结,如一些常用的JS 对象,基本数据结构,功能函数等,还有一些常用的设计模式. 目录: 众所周知,JavaScri ...

随机推荐

  1. windows下同时装了Python3和Python2,如何区分使用?

    1.前言 想学习Python3,但是暂时又离不开Python2.在Windows上如何让它们共存呢? 目前国内网站经常会让大家把其中一个python.exe改个名字,这样区分开两个可执行文件的名字,但 ...

  2. php自定义截取中文字符串-utf8版

    php自定义截取中文字符串-utf8版 UTF-8的编码范围(utf-8使用1-6个字节编码字符,实际上只使用了1-4字节): 1个字节:00——7F 2个字节:C080——DFBF 3个字符:E08 ...

  3. eclipse中springmvc框架出现404

    出现404报错信息: 1.我出现的错误是配置没问题,找了一圈发现自己单词拼错了,导致运行不了,出现404 2.配置问题: jar包:首先导入spring 的jar包15个,log4j两个,junit两 ...

  4. Unity 横版2D移动跳跃问题——关于一段跳与二段跳

    1.初始条件: 1.角色只绑定一个碰撞体,移动时施加力或给予速度,用跳跃次数JumpTimes或者bool值OnGround判断是否在地面. 2.只用一个tilemap搭建2D场景,因此所有tilem ...

  5. 分组取topN

    假设有这样一个文件,文件内容如下 class1 class2 class1 class1 class2 class2 class1 class2 class1 class2 要求按照班级分组取出每个班 ...

  6. 【SSM】自定义属性配置的使用

    首先,建立xxx.properties 文件在resource文件夹中,此处我们自定义的配置文件是oj-config.properties 然后,在applicationContext.xml中注册这 ...

  7. [学习笔记] 在Eclipse中使用Hibernate,并创建第一个工程

    在Eclipse中使用Hibernate 安装 Hibernate Tools 插件 https://tools.jboss.org/downloads/ Add the following URL ...

  8. windwos 10 安装flask

    1 安装python2.7.13 安装文件为:python-2.7.13.amd64.msi,因为python2.7.13中已经包含了pip. 在安装过程中选中[Add python.exe to P ...

  9. ACL2019: 《GraphRel: Modeling Text as Relational Graphs for Joint Entity and Relation Extraction》源码解析

    论文地址:<GraphRel: Modeling Text as Relational Graphs for Joint Entity and Relation Extraction> G ...

  10. SoapUI使用JDBC请求连接数据库及断言的使用

    SoapUI提供了用来配置JDBC数据库连接的选项,因此我们可以在测试中使用JDBC数据源.JDBC数据接收器和JDBC请求步骤. 为了能够配置数据连接,就必须有驱动程序和连接串,SoapUI中已经提 ...