Matplotlib常用绘图示例入门

时间:2017-11-04 22:05:49   收藏:0   阅读:343

一.Matplotlib介绍

Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等。Matplotlib使用NumPy进行数组运算,并调用一系列其他的Python库来实现硬件交互。

 

二.常用示例

环境:Jupyter(1.0.0) Ubuntu安装Jupyter Notebook

1.折线图

%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(9)
y = np.sin(x)
z = np.cos(x)
# marker数据点样式,linewidth线宽,linestyle线型样式,color颜色
plt.plot(x, y, marker="*", linewidth=3, linestyle="--", color="orange")
plt.plot(x, z)
# 设置X轴刻度,rotation旋转
plt.xticks(x, [str(i) + "K" for i in x], rotation=45)
plt.title(u"matplotlib")
plt.xlabel("height")
plt.ylabel("width")
# 设置图例
plt.legend(["Y","Z"], loc="upper right")
plt.grid(True)
plt.show()

技术分享

2.散点图

x = np.random.rand(10)
y = np.random.rand(10)
plt.scatter(x,y)
plt.show()

技术分享

3.柱状图

x = np.arange(10)
y = np.random.randint(0,30,10)
plt.bar(x, y)
plt.show()

技术分享

4.饼图

x = np.random.rand(3)
plt.pie(x)
plt.show()

技术分享

5.直方图

mean, sigma = 0, 1
x = mean + sigma * np.random.randn(10000)
plt.hist(x,50)
plt.show()

 技术分享

6.子图

# figsize绘图对象的宽度和高度,单位为英寸,dpi绘图对象的分辨率,即每英寸多少个像素,缺省值为80
plt.figure(figsize=(8,6),dpi=100)

# 一个Figure对象可以包含多个子图Axes,subplot将整个绘图区域等分为numRows行*numCols列个子区域,
# 按照从左到右,从上到下的顺序对每个子区域进行编号
A = plt.subplot(2,2,1)
plt.plot([0,1],[0,1], color="red")

plt.subplot(2,2,2)
plt.title("B")
plt.plot([0,1],[0,1], color="green")

plt.subplot(2,1,2)
plt.title("C")
plt.plot(np.arange(10), np.random.rand(10), color="orange")

# 选择子图A
plt.sca(A)
plt.title("A")

plt.show()

技术分享

原文:http://www.cnblogs.com/faramita2016/p/7784699.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!