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. P3665 [USACO17OPEN]Switch Grass

    题目描述 N个点M条边的无向图,每个点有一个初始颜色,每次改变一个点的颜色,求改变后整张图上颜色不同的点之间的距离最小值. 思路 考虑整张图的距离最小值一定是一条边,而不可能是一条路径,那么显然这条边 ...

  2. 二、安装JDK - Java软件的安装

    jdk是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序. 1.安装包的下载:http://pan.baidu.com/s/1mgh58ve (该安装包是绿色的,解压 ...

  3. 最新JetBrains PyCharm 使用教程--安装教程(一)

    安装过程 ​ ​ ​ ​ ​ ​ ​ Pycharm  激活码 license server选项里边输入:http://intellij.mandroid.cn/ ​

  4. html5不能播放视频的方法

    html5不能播放视频的方法H5不能播放视频的方法 格式工厂转换成 AVC 注意音频流 采样率和比特率 设置低一点 不然播放会卡住 还有点击播放必须要click 不要用touchstart没效果的

  5. PHP failed to ptrace(PEEKDATA) pid 13659: Input/output error错误解决方法

    PHP failed to ptrace(PEEKDATA) pid 13659: Input/output error错误解决方法 现在改linux内核文件打开限制<pre>ulimit ...

  6. supervisord进程管理

    一:简介 supervisord是一个进程管理工具,提供web页面管理,能对进程进行自动重启等操作. 优点: - 可以将非后台运行程序后台运行 - 自动监控,重启进程 缺点: - 不能管理后台运行程序 ...

  7. ActiveMQ消息队列从入门到实践(1)—JMS的概念和JMS消息模型

    1. 面向消息的中间件 1.1 什么是MOM 面向消息的中间件,Message Oriented Middleware,简称MOM,中文简称消息中间件,利用高效可靠的消息传递机制进行平台无关的数据交流 ...

  8. Docker学习-Docker搭建Consul集群

    1.环境准备 Linux机器三台 网络互通配置可以参考 https://www.cnblogs.com/woxpp/p/11858257.html 192.168.50.21 192.168.50.2 ...

  9. VMware Workstation Pro(15.5)下安装Windows_Server_2008_R2

    一.新建虚拟机 1.打开VMware Workstation Pro 15.5虚拟机,点击新建虚拟机 2.选择典型(推荐),单击下一步 3.选最后一个 稍后安装操作系统,点击下一步 4.进来页面,选择 ...

  10. 【NServiceBus】什么是Saga,Saga能做什么

    前言           Saga单词翻译过来是指尤指古代挪威或冰岛讲述冒险经历和英雄业绩的长篇故事,对,这里强调长篇故事.许多系统都存在长时间运行的业务流程,NServiceBus使用基于事件驱动的 ...