绘制散点图命令格式
plt.scatter(x,y)
举例说明
绘制三月份 十月份的每一天的 气温变化情况
from matplotlib import pyplot as plt
from matplotlib import font_manager
import numpy as np
my_font = font_manager.FontProperties(fname='C:/Windows/Fonts/simhei.ttf')
y_3 = [10,12,10,11,20,14,13,12,11,12,11,12,13,14,15,15,12,17,18,12,15,16,15,12,13,17,18,16,15,12,11]
y_10 = [22,23,25,24,26,22,23,21,20,22,23,24,25,21,22,23,21,22,22,21,23,20,20,21,22,23,25,24,27,24,22]
x_3 =range(1,32)
x_10 = range(51,82)
#设置图片大小
plt.figure(figsize=(12,12),dpi=80)
#调整x轴刻度
x_ = list(x_3) +list(x_10)
x_tick_labels = ['三月{}日'.format(i) for i in x_3]
x_tick_labels += ['十月{}日'.format(i-30) for i in x_10]
plt.xticks(x_[::3],x_tick_labels[::3],fontproperties=my_font,rotation=45)
# 添加描述信息
plt.xlabel('时间',fontproperties=my_font)
plt.ylabel('温度',fontproperties=my_font)
plt.title('温度变化',fontproperties=my_font)
#使用scatter绘制散点图
plt.scatter(x_3,y_3,label='三月份')
plt.scatter(x_10,y_10,label='十月份')
# 添加图例
plt.legend(loc='upper left',prop=my_font)
# 展示
plt.show()
绘制效果图片展示
条形图绘制指令
plt.scatter(x,y)
举例说明
统计电影票房
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname='C:/Windows/Fonts/simhei.ttf')
a = ['战狼2','速度与激情9','功夫瑜伽','西游降魔','变形金刚','摔跤吧,','加勒比海盗','天下无贼','金刚狼3:殊死搏斗']
b = [53.01,25.03,12.03,45.03,25.89,5.23,52.39,12.89,55]
# 设置图片大小
plt.figure(figsize=(10,10),dpi=80)
# 调整x轴刻度
plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=45)
# 添加描述信息
plt.xlabel('电影名称',fontproperties=my_font)
plt.ylabel('电影票房',fontproperties=my_font,rotation=90)
plt.title('电影票房统计',fontproperties=my_font)
# 绘制条形图
plt.bar(range(len(a)),b,color='pink',width=0.3)
plt.show()
绘图效果展示
绘制横着的条形图
指令
plt.barh(x,y)
举例说明
# 绘制横着的条形图
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname='C:/Windows/Fonts/simhei.ttf')
a = ['战狼2','速度与激情9','功夫瑜伽','西游降魔','变形金刚','摔跤吧,','加勒比海盗','天下无贼','金刚狼3:殊死搏斗']
b = [53.01,25.03,12.03,45.03,25.89,5.23,52.39,12.89,55]
# 设置图片大小
plt.figure(figsize=(10,10),dpi=80)
# 调整x轴刻度
plt.yticks(range(len(a)),a,fontproperties=my_font)
# 添加描述信息
plt.ylabel('电影名称',fontproperties=my_font)
plt.xlabel('电影票房',fontproperties=my_font,rotation=90)
plt.title('电影票房统计',fontproperties=my_font)
plt.grid(alpha=0.3)
# 绘制条形图
plt.barh(range(len(a)),b,color='pink',height=0.3)
plt.show()
绘制效果展示
绘制多个条形图
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname='C:/Windows/Fonts/simhei.ttf')
a = ['猩球崛起3:世界末日','战狼2','敦刻尔克','蜘蛛侠:新英雄归来']
b_1 = [22,23,24,11,]
b_2 = [12,13,41,22,]
b_3 = [12,10,0.5,23]
bar_width = 0.2
x_1 = list(range(len(a)))
x_2 = [i+bar_width for i in x_1]
x_3 = [i+bar_width*2 for i in x_1]
# 设置图片大小
plt.figure(figsize=(10,10),dpi=80)
# 设置x轴刻度
plt.xticks(x_2,a,fontproperties=my_font)
plt.bar(range(len(a)),b_1,width=bar_width,label='九月一日')
plt.bar(x_2,b_2,width=bar_width,label='九月二日')
plt.bar(x_3,b_3,width=bar_width,label='九月三日')
# 显示图例
plt.legend(loc='upper left',prop=my_font)
plt.show()