Python篇(二):matplotlib中pyplot的使用简介
2017-09-28 14:09
716 查看
Python提供了类似于MATLAB的2D绘图库,在使用时会发现编译通过但图片show不出来的情况。如
此时只需要增加如下语句即可show出图片
example_1
![](https://img-blog.csdn.net/20170928145128392?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGVlemhh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
example_2 子图
![](https://img-blog.csdn.net/20171010163741862?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGVlemhh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
example_3[b]
图上加文字注释[/b]
![](https://img-blog.csdn.net/20171010164651822?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGVlemhh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
example_4 箭头标注
![](https://img-blog.csdn.net/20171010164657656?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGVlemhh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
example_5 散点图
![](https://img-blog.csdn.net/20170928145134821?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGVlemhh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
example_6
读文本数据绘图
![](https://img-blog.csdn.net/20170928172401755?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGVlemhh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
数据来源:http://kinetics.nist.gov/janaf/html/C-067.txt
from numpy import array a = array([1,2,3,4]) from matplotlib import pyplot pyplot.plot(a,3*a)
此时只需要增加如下语句即可show出图片
pyplot.show()
example_1
import matplotlib.pyplot as plt from numpy import * plt.close('all') pi = 3.1415926 x = linspace(0, 2*pi, 50) y = sin(x) z = cos(x) plt.plot(x, y, 'g-o', label='sin') plt.plot(x, z, 'r-^', label='cos') # plt.legend(['sin', 'cos']) plt.legend() plt.axis([0, 7, -1.2, 1.2]) plt.xlabel('radians', fontsize='large') plt.ylabel('amplitude', fontsize='large') plt.title('eg title') plt.grid()
example_2 子图
import numpy as np import matplotlib.pyplot as plt def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure(1) plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()
example_3[b]
图上加文字注释[/b]
import numpy as np import matplotlib.pyplot as plt mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()
example_4 箭头标注
import numpy as np import matplotlib.pyplot as plt ax = plt.subplot(111) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2,2) plt.show()
example_5 散点图
from matplotlib import pyplot from numpy.random import rand x = rand(200) y = rand(200) size = rand(200) * 30 color = rand(200) pyplot.scatter(x, y, size, color) pyplot.colorbar() pyplot.show()
example_6
读文本数据绘图
# -*- coding:utf-8 -*- import sys import numpy as np import matplotlib.pyplot as plt reload(sys) sys.setdefaultencoding('utf-8') np.set_printoptions(precision=2, suppress=True) data = np.genfromtxt("JANAF_CH4.txt", delimiter="\t", # TAB 分隔 skiprows=1, # 忽略首行 names=True, # 读入属性 missing_values="INFINITE", # 缺失值 filling_values=np.inf) # 填充缺失值 for row in data[:7]: print "{}\t{}".format(row['TK'], row['Cp']) print "...\t..." p = plt.plot(data['TK'], data['Cp'], 'kx') t = plt.title("JANAF data for Methane $CH_4$") a = plt.axis([0, 6000, 30, 120]) x = plt.xlabel("Temperature (K)") y = plt.ylabel(r"$C_p$ ($\frac{kJ}{kg K}$)") plt.show()
数据来源:http://kinetics.nist.gov/janaf/html/C-067.txt
相关文章推荐
- python中matplotlib.pyplot使用简介
- matplotlib(一)——pyplot使用简介
- matplotlib(一)——pyplot使用简介
- matplotlib(一)——pyplot使用简介
- Matplotlib简介和pyplot的简单使用——Artists
- Matplotlib简介和pyplot的简单使用——直方统计图和轮廓曲线
- Matplotlib简介和pyplot的简单使用——文本标注和箭头
- Matplotlib简介和pyplot的简单使用
- Matplotlib简介和pyplot的简单使用——subplot
- python: 使用matplotlib的pyplot绘制图表
- Python matplotlib简介 Pyplot教程
- Matplotlib简介和pyplot的简单使用——局部放大图
- 浅谈Matplotlib简介和pyplot的简单使用――文本标注和箭头
- Python:matplotlib中pyplot的使用方法
- Python中matplotlib pyplot常用的一些使用方法[Python 3.x]
- 使用python的matplotlib(pyplot)画折线图和柱状图
- Matplotlib简介和pyplot的简单使用——直方统计图和轮廓曲线
- Matplotlib简介和pyplot的简单使用——输出eps格式图像
- python使用matplotlib绘图 -- barChart
- 使用 python Matplotlib 库绘图