目录
一、折线图
#折线图
import numpy as np
from matplotlib import pyplot as plt
data = np.arange(1,13)
value = [3,4,1,6,8,5,9,8,7,6,5,5]
plt.plot(data,value,marker='h') #marker 表示每个数据点的样式
plt.xticks(rotation = 45)
plt.xlabel("Month") #x轴标签
plt.ylabel('Value') #y轴标签
plt.title('Line chart',fontsize="large",fontweight='bold',color='blue') #设置标题 名字 字体大小 字体粗细:黑体字 颜色:蓝色
plt.show()
输出结果:
二、条形图
#条形图
from matplotlib import pyplot as plt
import numpy as np
student = np.arange(1,8)
time = [7,6,7,6,5,4,7]
plt.bar(student,time,alpha=0.8)
plt.xticks(rotation=45)
plt.xlabel('Student')
plt.ylabel('Time')
plt.title('Bar chart',fontsize='large',fontweight='bold',color='blue')
plt.show()
输出结果:
三、直方图
#直方图
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
matplotlib.rcParams['font.sans-serif']=['SimHei'] #用黑体字显示中文
matplotlib.rcParams['axes.unicode_minus']=False #正常显示负号
data = np.random.randn(500)
plt.hist(data,bins=40,facecolor="c",edgecolor='black',alpha=1)
plt.xlabel('区间',loc='center') #轴标签居中
plt.ylabel('频率',loc='top') #轴标签在顶部输出
plt.title("Histogram",fontsize='large',fontweight="bold",color='blue') #设置标题字体粗细颜色
plt.show()
输出结果:
四、散点图
#散点图
from matplotlib import pyplot as plt
import numpy as np
number = 100
x = np.random.randn(number) #传入100个随机数
y = np.random.randn(number)
x1 = np.random.randn(number)
y1 = np.random.randn(number)
plt.scatter(x,y,c="y",marker=">") #传入数据,设置样式
plt.scatter(x1,y1,c='k',marker='o')
plt.title('Scatter plot',fontsize='large',fontweight='bold',color='blue') #设置标题字体粗细颜色
plt.show()
输出结果:
五、饼图
#饼图
from matplotlib import pyplot as plt
import numpy as np
p = np.array([0.45,0.15,0.2,0.15,0.05])
colors = ['red','yellow','green','blue','purple']
labels = [">89","80~89","70~79","60~69","<60"]
plt.pie(p,labels = labels,autopct="%1.2f%%",colors = colors,labeldistance = 1.1,pctdistance=0.5,explode=[0.1,0.1,0.1,0.2,0.1],shadow=True,startangle=90)
plt.axis("equal")
plt.title('Pie chart',fontsize="large",fontweight='bold',color='blue') #设置标题字体粗细颜色
plt.show()
输出结果:
六、3D散点图
#3D散点图
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(1,1,1,projection="3d")
number = 100
x = np.random.randn(number) #生成100个随机数并传入x,y,z,x1,y1,z1
y = np.random.randn(number)
z = np.random.randn(number)
x1 = np.random.randn(number)
y1 = np.random.randn(number)
z1 = np.random.randn(number)
ax.scatter(x,y,z,c="c",marker="p") #传入数据,设置样式,画出图形
ax.scatter(x1,y1,z1,c='r',marker='*')
ax.set_xlabel('x Label') #设置x,y,z轴标签
ax.set_ylabel('y Label')
ax.set_zlabel('z Label')
plt.show()
输出结果:
七、3D条形图
#3D条形图
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure() #设置画布
ax1 = fig.add_subplot(121,projection='3d')
ax2 = fig.add_subplot(122,projection='3d')
x = [1,3,5,7] #写入数据
y = [3,6,9,12]
bottom = [1,1,1,1]
top = [1,3,1,1]
width = [1,1,3,1]
depth = [1,1,1,3]
ax1.bar3d(x,y,bottom,width,depth,top,shade=True) #画3d图形
ax1.set_xlabel('x Label') #设置x,y,z轴标签
ax1.set_ylabel('y Label')
ax1.set_zlabel('z label')
ax1.set_title('Shaded') #设置标题
ax2.bar3d(x,y,bottom,width,depth,top,shade=False) #画3d图形
ax2.set_xlabel('x Label') #设置x,y,z轴标签
ax2.set_ylabel('y Label')
ax2.set_zlabel('z Label')
ax2.set_title('Not Shaded') #设置标题
plt.show()
输出结果: