plt画图工具(二维图形)

1.直线图

import numpy as np 
from matplotlib import pyplot as plt 
 
x = np.arange(1,11) 
y =  2  * x +  5 
plt.title("Matplotlib demo") 
plt.xlabel("x axis caption") 
plt.ylabel("y axis caption") 
plt.plot(x,y) 
plt.grid(True)
plt.show()
View Code

plt.grid设置网格线

2.散点图

plt.scatter(x,y,marker='o',color='r')

color 颜色参数 b(blue) g(grenn) r(red) c(cyan) m(magenta) y(yellow) k(boack) w(white)

marker 点的图形样式

3.柱状图

plt.bar(x, y, color='green',width=0.8)

width柱子的宽度

plt.barh(x,y)垂直的柱状图

4.饼图

plt.pie([5,6,7], labels=['a','b','c'],autopct='%0.f%%',shadow=True)  

5.多图形式

import matplotlib.pyplot as plt
import numpy as np

# Plot circle of radius 3.

an = np.linspace(0, 2 * np.pi, 100)
fig, axs = plt.subplots(2, 2)

axs[0, 0].plot(3 * np.cos(an), 3 * np.sin(an))
axs[0, 0].set_title('not equal, looks like ellipse', fontsize=10)

axs[0, 1].plot(3 * np.cos(an), 3 * np.sin(an))
axs[0, 1].axis('equal')
axs[0, 1].set_title('equal, looks like circle', fontsize=10)

axs[1, 0].plot(3 * np.cos(an), 3 * np.sin(an))
axs[1, 0].axis('equal')
axs[1, 0].set(xlim=(-3, 3), ylim=(-3, 3))
axs[1, 0].set_title('still a circle, even after changing limits', fontsize=10)

axs[1, 1].plot(3 * np.cos(an), 3 * np.sin(an))
axs[1, 1].set_aspect('equal', 'box')
axs[1, 1].set_title('still a circle, auto-adjusted data limits', fontsize=10)

fig.tight_layout()

plt.show()
View Code

按比例切分

import numpy as np 
import matplotlib.pyplot as plt 
# 计算正弦和余弦曲线上的点的 x 和 y 坐标 
x = np.arange(0,  3  * np.pi,  0.1) 
y_sin = np.sin(x) 
y_cos = np.cos(x)  
# 建立 subplot 网格,高为 2,宽为 1  
# 激活第一个 subplot
plt.subplot(2,  1,  1)  
# 绘制第一个图像 
plt.plot(x, y_sin) 
plt.title('Sine')  
# 将第二个 subplot 激活,并绘制第二个图像
plt.subplot(2,  1,  2) 
plt.plot(x, y_cos) 
plt.title('Cosine')  
# 展示图像
plt.show()
View Code

6.画函数

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.mathtext as mathtext
import matplotlib

matplotlib.rc('image', origin='upper')

parser = mathtext.MathTextParser("Bitmap")

parser.to_png('test3.png',r'$ \alpha_1 \beta_j \pi \lambda \omega $',color='green', fontsize=30, dpi=100)
img = plt.imread('test3.png')  

plt.imshow(img)
plt.show()
View Code

猜你喜欢

转载自www.cnblogs.com/yangyang12138/p/12549932.html