Python篇(二):matplotlib中pyplot的使用简介
来源:互联网 发布:如何联系淘宝小二 编辑:程序博客网 时间:2024/05/21 11:21
Python提供了类似于MATLAB的2D绘图库,在使用时会发现编译通过但图片show不出来的情况。如
from numpy import arraya = array([1,2,3,4])from matplotlib import pyplotpyplot.plot(a,3*a)
此时只需要增加如下语句即可show出图片
pyplot.show()
example_1
import matplotlib.pyplot as pltfrom numpy import *plt.close('all')pi = 3.1415926x = 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 npimport matplotlib.pyplot as pltdef 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 图上加文字注释
import numpy as npimport matplotlib.pyplot as pltmu, sigma = 100, 15x = mu + sigma * np.random.randn(10000)# the histogram of the datan, 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 npimport matplotlib.pyplot as pltax = 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 pyplotfrom numpy.random import randx = rand(200)y = rand(200)size = rand(200) * 30color = rand(200)pyplot.scatter(x, y, size, color)pyplot.colorbar()pyplot.show()
example_6读文本数据绘图
# -*- coding:utf-8 -*-import sysimport numpy as npimport matplotlib.pyplot as pltreload(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
阅读全文
0 0
- Python篇(二):matplotlib中pyplot的使用简介
- Python:matplotlib中pyplot的使用方法
- Python matplotlib简介 Pyplot教程
- 二、Matplotlib系列---pyplot的plot( )函数
- python matplotlib.pyplot importerror
- 【python】matplotlib.pyplot介绍
- Python---matplotlib.pyplot
- 【python】matplotlib.pyplot入门
- python之Matplotlib.pyplot
- Matplotlib简介和pyplot的简单使用——直方统计图和轮廓曲线
- Python中matplotlib pyplot常用的一些使用方法[Python 3.x]
- matplotlib(一)——pyplot使用简介
- matplotlib(一)——pyplot使用简介
- matplotlib(一)——pyplot使用简介
- Python matplotlib pyplot中title() xlabel() ylabel()无法显示在中文(方框乱码)的解决办法
- Python matplotlib pyplot中title() xlabel() ylabel()无法显示在中文(方框乱码)的解决办法
- CentOS中python输入import matplotlib.pyplot提示有错误
- CentOS中python输入import matplotlib.pyplot提示有错误
- Makefile基本一之大体语法组成
- HTML之动态添加、删除
- 在屏幕上用“*”输出指定行数的菱形图案
- SSM的config文件springmvc.xml
- go学习(十一)——Go 语言自动化测试
- Python篇(二):matplotlib中pyplot的使用简介
- html页面获取参数
- #文末福利|云栖大会门票限时赠送# 10.11云栖大会Clouder Lab “十分钟搭建共享应用”,手把手教你Serverless架构下的应用实现
- oracle 统计分析函数
- 二分图匹配
- Virtualbox主机和虚拟机之间文件夹共享及双向拷贝(Windows<->Windows, Windows<->Linux)
- centos安装node试试
- 【wxpython】py:603: wxPyDeprecationWarning: Using deprecated class PySimpleApp.
- demo_5. python3.6字符串操作总结