# 网站上有各种各样的图,直接copy进行修改。
# https://matplotlib.org/gallery/index.html
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x**2)
plt.plot(x,y,"r-",label="$sin(x)$",linewidth=3)
plt.plot(x,z,"b--",label="$cos(x^2)$")
plt.xlabel("Times(s)")
plt.ylabel("Volt")
plt.title("PyPlot First Example")
plt.legend() #图例
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/d556952c172bbd0d922cc2429eaf8d1c.png)
# pyplot的绘图区域
# plt.subplot(nrows,ncols,plot number)
import matplotlib.pyplot as plt
plt.subplot(2,1,1)
plt.xticks([]),plt.yticks([])
plt.text(0.5,0.5,'subplot(2,1,1)',ha = 'center', va = 'center',size = 20, alpha = 0.5)
plt.subplot(2,1,2)
plt.xticks([]),plt.yticks([])
plt.text(0.5,0.5,'subplot(2,1,2)',ha = 'center', va = 'center',size = 20, alpha = 0.5)
#plt.saveig('1.png',dpi = 64)
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/b4fff5ebac75b2f7ccf7267065896f09.png)
#折线图
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
xx = [1,2,3,4,5,3,7,8,9,2]
yy = [3,4,5,6,1,9,7,8,5,3]
zz = [2,9,5,6,1,7,3,4,2,7]
plt.plot(xx, color='r', linestyle='-',label="data1")
plt.plot(yy, color='g', linestyle='--',label="data2")
plt.plot(zz, color='b', linestyle=':',label="data3")
# lengend() 的位置
#[‘upper left’ , ‘upper center’, ‘upper right’,
#‘center left’, ‘center’, ‘center right’,
#‘lower left’, ‘lower center’, ‘lower right’]
plt.legend(loc = 'upper left')
plt.xlabel("x轴名称",fontproperties="simhei")
plt.ylabel("y轴名称",fontproperties="simhei")
plt.title("折线图",fontproperties="simhei")
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/0f3a36d30b5311a3611751b479470831.png)
# 散点图
import matplotlib.pyplot as plt
import numpy as np
N = 10
x1 = np.random.rand(N)
y1 = np.random.rand(N)
x2 = np.random.rand(N)
y2 = np.random.rand(N)
#size = (30 * np.random.rand(N)) ** 2
color = np.random.rand(N)
# s = size
plt.scatter(x1,y1,alpha = 0.5,c = color, marker='^',label = "triangle")
plt.scatter(x2,y2,alpha = 0.5,c = color, marker='o',label = "circle")
plt.legend(loc = 'best')
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/336cecdb3a31ab2b53b1485c3e2837ef.png)
#堆叠柱状图
%matplotlib inline
import matplotlib.pyplot as plt
name_list = ["Monday","Tuesday","Friday","Sunday"]
num_list = [1.5,0.6,7.6,5]
num_list2 = [1,2,3,4]
plt.bar(range(len(num_list)),num_list,color = 'r',tick_label=name_list, label= "boys")
plt.bar(range(len(num_list)),num_list2,color = 'g',tick_label=name_list, label= "girls")
plt.legend(loc='best')
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/b393cf95c93d705e1c2614fe80cbf71a.png)
# 横向柱状图
import matplotlib.pyplot as plt
name_list = ["Monday","Tuesday","Friday","Sunday"]
num_list = [1.5,0.6,7.6,5]
num_list2 = [1,2,3,4]
plt.barh(range(len(num_list)),num_list,color = 'r',tick_label=name_list, label= "boys")
plt.barh(range(len(num_list)),num_list2,color = 'g',tick_label=name_list, label= "girls")
plt.legend(loc='best')
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/00456e38741c31a184bf4a8c2eda731d.png)
#并行柱状图
import matplotlib.pyplot as plt
name_list = ["Monday","Tuesday","Friday","Sunday"]
num_list = [1.5,0.6,7.6,5]
num_list2 = [1,2,3,4]
x = list(range(len(num_list)))
total_width = 0.8
n = 2
width = total_width / n
plt.bar(x,num_list,width = width, label = "boys")
for i in range(len(x)):
x[i] = x[i] + width
plt.bar(x,num_list,width = width, label = "girls", tick_label=name_list)
plt.legend(loc = "best")
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/e7ef80b079c1718feae065d9636e1050.png)
# 饼状图
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来显示中文标签
labels = 'A','B','C','D'
sizes = [0.1,0.2,0.3,0.5]
explode= (0,0,0.2,0)
colors = ['r','g','b','c']
plt.pie(sizes,labels = labels, explode = explode, colors = colors, autopct = '%1.2f%%')
plt.title('简单饼状图')
plt.legend(loc='upper right', fontsize = 8, borderaxespad = 0.3)
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/6702acaa8874634ddb4ff7b97d3d2fc8.png)
# 雷达图 戴布拉图 蜘蛛网图
# 常用于对多项指标的全面分析。
import numpy as np
import matplotlib.pyplot as plt
courses = ['C++','Python','高等数学','大学英语','软件工程',
'组成原理','操作系统','网络工程']
scores = [82,95,72,88,46,58,70,80]
datalength = len(scores)
angles = np.linspace(0, 2*np.pi, datalength, endpoint= False)
scores.append(scores[0])
angles = np.append(angles,angles[0])
plt.polar(angles, scores, 'rv-', lw = 2)
plt.thetagrids(angles*180/np.pi, courses, fontproperties = 'simhei')
plt.fill(angles, scores, facecolor = 'r',alpha = 0.5)
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/a22123f1a83adc212607175cf3546988.png)
# 三维图
# 想要绘制三维图形, 需要
# from mpl_toolkits.mplot3d import Axes3D
# 两种方式之一声明要创建三维子图:
# ax = fig.gca(projection = '3d')
# ax = plt.subplot(111, projection = '3d')
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
# 测试数据
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-4, 4, 100)*0.3
r = z**4 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, 'b^-', label='3D Picture Test')
mpl.rcParams['legend.fontsize'] = 8
ax.legend()
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/31789e71037b2bfe804ec1552d6a913f.png)
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
x = np.random.randint(0, 40, 10)
y = np.random.randint(0, 40, 10)
z = 80 * abs(np.sin(x + y))
ax = plt.subplot(projection ='3d')
for xx, yy, zz in zip(x, y, z):
color = np.random.random(3)
ax.bar3d(xx, yy, 0, dx=1, dy=1, dz=zz, color = color)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/752d38245eae5d773170644545a49839.png)