首页 > 技术文章 > python基于matplotlib绘图

liutongqing 2017-05-22 21:11 原文

import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from mpl_toolkits.mplot3d import Axes3D

font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)  # 设置字体

# 极坐标绘图
r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r

plt.figure()  
plt.subplot(221, polar = True)  # 221位置,极坐标绘图
plt.plot(theta, r)   # 绘图
plt.grid(True)       # 显示网格
plt.title(u'极坐标图', fontproperties=font) # 设置标题


# 极坐标绘图
theta=np.arange(0,2*np.pi,0.02)  

plt.subplot(222, polar=True)   # 222位置,极坐标绘图
plt.plot(theta, np.cos(5*theta), '--', lw=2)  # 绘图,虚线,线宽为2
plt.plot(theta, 2*np.cos(4*theta), lw=2)     # 绘图,线宽为2
plt.rgrids(np.arange(0.5,2,0.5), angle=45)  
plt.thetagrids([0,45,90])  
plt.title(u'极坐标图', fontproperties=font)

# 直角坐标绘图
x = np.linspace(0, 3, 500)
y1 = np.sin(4 * np.pi * x) * x
y2 = np.exp(0.5*x)

plt.subplot(223)       # 223位置
plt.plot(x,y1,'r',label = 'Line 1')  # 绘制x-y图像
plt.plot(x,y2,'b',label = 'Line 2')  # 绘制x-y图像
plt.xlim(0, 3)  # 设置横坐标范围为0~3
plt.xlabel("x") # x轴的标签
plt.ylabel("y") # y轴的标签
plt.title(u'直角坐标图', fontproperties=font)  # 设置标题
plt.legend() # 标注曲线

# 折线图
x = np.linspace(0, 2 * np.pi, 10)
y1, y2 = np.sin(x), np.cos(x)

plt.subplot(224)  # 224位置绘图
plt.plot(x, y1, marker='o', mec='r', mfc='w') # 绘制圆形点
plt.plot(x, y2, marker='*', ms=10)            # 绘制星形点
plt.title(u'折线图', fontproperties=font)  # 设置标题

plt.savefig("pic1.png") # 保存图像为pic1.png
plt.show()  # 显示图像

# 绘制散点图
plt.figure()  
x = np.random.random(100)  # 随机生成100个数
y = np.random.random(100)  # 随机生成100个数
plt.scatter(x,y,c='g',marker=(5,1))  # 绘制绿色五角星散点
plt.xlim(0,1)   # 设置x轴范围
plt.ylim(0,1)   # 设置y轴范围
plt.title(u'散点图', fontproperties=font) # 设置标题
plt.savefig("pic2.png") # 保存图像为pic2.png
plt.show()   # 显示图像

# 绘制饼图
labels=['Class 1','Class 2','Class 3','Class 4','Class 5','Class 6']  
#data = [222,242,345,664,454,334] 
data = 100*np.random.random(6)  # 随机生成6个数
x = np.arange(len(data))  #0~5

plt.figure()
plt.pie(data, labels=labels, autopct='%1.2f%%') #画饼图(数据,数据对应的标签,百分数保留两位小数点)  
plt.title(u'饼图', fontproperties=font)   # 设置标题
plt.savefig("pic3.png") # 保存图像为pic3.png
plt.show()   

# 绘制条形图
plt.figure()
plt.bar(x, data)  # 绘制条形图
plt.plot(x, data, c='r')  # 绘制折线图,颜色为红色
plt.title(u'条形图和折线图', fontproperties=font)  # 设置标题 
plt.savefig("pic4.png") # 保存图像为pic4.png
plt.show()   # 显示图像

# 三维曲线绘图
fig = plt.figure()
ax = Axes3D(fig)
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve') #绘制三维曲线
ax.set_title(u'三维曲线绘图', fontproperties=font)  # 设置标题
plt.savefig("pic5.png") # 保存图像为pic5.png
plt.show()  # 显示图像

# 三维曲面绘图
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

ax.plot_surface(X, Y, Z, cmap='rainbow') # 绘制曲面
ax.set_xlabel('x') # 设置x轴标签
ax.set_ylabel('y') # 设置y轴标签
ax.set_zlabel('z') # 设置z轴标签
ax.set_title(u'三维曲面', fontproperties=font) # 设置图像标题
plt.savefig("pic6.png") # 保存图像为pic6.png
plt.show() # 显示图像

pic1.png

pic2.png

pic3.png

pic4.png

pic5.png

pic6.png

 

推荐阅读