Python篇(二):matplotlib中pyplot的使用简介

Python提供了类似于MATLAB的2D绘图库,在使用时会发现编译通过但图片show不出来的情况。如

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()
Python篇(二):matplotlib中pyplot的使用简介_第1张图片

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()

Python篇(二):matplotlib中pyplot的使用简介_第2张图片


example_3  图上加文字注释

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()

Python篇(二):matplotlib中pyplot的使用简介_第3张图片


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()

Python篇(二):matplotlib中pyplot的使用简介_第4张图片



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()

Python篇(二):matplotlib中pyplot的使用简介_第5张图片

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()

Python篇(二):matplotlib中pyplot的使用简介_第6张图片                                                  

数据来源:http://kinetics.nist.gov/janaf/html/C-067.txt


你可能感兴趣的:(Python)