matplotlib模块

画图

条形图

import matplotlib.pyplot as plt # 默认支持英文,不支持中文
from matplotlib.font_manager import FontProperties
font = FontProperties(fname='D:\msyh.ttc')
classes = ['3班','4班','5班','6班']
students = [50,60,55,67]

ind = range(len(classes))

# plt.bar(ind,students,color='r')
# plt.xticks(ind,classes,fontproperties= font)
# plt.show()
plt.style.use('ggplot')
plt.bar(ind,students,color='darkblue')
plt.xticks(ind,classes,fontproperties= font)
plt.xlabel('班级',fontproperties=font,fontsize=15)
plt.ylabel('人数',fontproperties= font,fontsize=15)
plt.title('班级-人数',fontproperties=font,fontsize=20)
plt.show()

直方图

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
# %matplotlib inline  # 如果没有这个,不会显示图片,有这个会显示图片(只针对jupyter)
font = FontProperties(fname='D:\msyh.ttc')
mu1,mu2,sigma = 50,100,10
x1 = mu1+sigma*np.random.randn(100000)  # 符合正太分布的随机数据
x2 = mu2+sigma*np.random.randn(100000)
# plt.hist(x1,bins=50)# 每50个数据一根柱子
# plt.show()
fig = plt.figure()
 
ax1 = fig.add_subplot(1,2,1) # 一行2列 第一个
ax1.hist(x1,bins=50,color='yellow')
ax1.set_title('黄色的',fontproperties=font)

ax2 = fig.add_subplot(1,2,2)  # 一行2列 第二个
ax2.hist(x2,bins=100,color='green')
ax2.set_title('绿色的',fontproperties=font)

fig.suptitle('大标题',fontproperties=font,fontsize=20)

plt.show()

折线图

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
# %matplotlib inline  # 如果没有这个,不会显示图片,有这个会显示图片(只针对jupyter)
font = FontProperties(fname='D:\msyh.ttc')
x1 = np.random.randn(1,40).cumsum()
x2 = np.random.randn(1,40).cumsum()
x3 = np.random.randn(1,40).cumsum()
x4 = np.random.randn(1,40).cumsum()

plt.plot(x1,marker='o',color='r',label='红线',linestyle='--')
plt.plot(x2,marker='*',color='y',label='黄线',linestyle='-.')
plt.plot(x3,marker='s',color='green',label='绿色',linestyle=':')
plt.plot(x4,marker='x',color='b',label='蓝色',linestyle='-')


plt.legend(prop=font)  # label的字体的需要在这里换
plt.show()

散点图加直线图

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
# %matplotlib inline  # 如果没有这个,不会显示图片,有这个会显示图片(只针对jupyter)
font = FontProperties(fname='D:\msyh.ttc')
y = x1**2
plt.scatter(x1,y,s=100)
plt.show()
posted on 2019-08-19 16:37  黑糖A  阅读(100)  评论(0编辑  收藏  举报