一、折线图
1.一个简单的例子(一图单线)
# 导包
import matplotlib.pyplot as plt
import random
from matplotlib import font_manager
# x、y轴赋值
x = range(1, 32)
y = [random.randint(25,42) for i in x]
# 设置画布的大小
plt.figure(figsize=(10,5), dpi=80)
# 设置字体,以支持中文显示
my_font = font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=5)
# 画图
plt.plot(x, y, marker='o')
# 绘制刻度
plt.xticks(x, size=10)
y_ticks_label = ['{}℃'.format(i) for i in range(min(y), max(y)+1)]
plt.yticks(range(min(y), max(y)+1), y_ticks_label, size=10)
# 设置坐标轴标题
plt.xlabel('时间', fontproperties=my_font, size=12, rotation=30)
plt.ylabel('温度', fontproperties=my_font, size=12)
# 设置图标标题
plt.title('xx市7月份气温随时间的变化', fontproperties=my_font, size=20, color='red')
# 保存
plt.savefig('./tp03.png')
# 展示图
plt.show()
效果图:
2.稍微复杂的例子(一图多线)
# 导包
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 设置x,y的值
x = range(12, 23)
y1 = [148, 155, 158, 163, 167, 173, 178, 183, 184, 183, 185]
y2 = [148, 156, 158, 163, 164, 165, 166, 166, 166, 167, 168]
# 设置画布大小
plt.figure(figsize=(20,10), dpi=80)
# 设置语言,以支持中文显示
my_font = font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=18)
# 画图
plt.plot(x, y1, label='男生', color='green', marker='o')
plt.plot(x, y2, label='女生', color='red', marker='o')
# 绘制坐标轴刻度
x_ticks_label = ['{}岁'.format(i) for i in x]
plt.xticks(x, x_ticks_label, fontproperties=my_font)
# 设置坐标轴标题
plt.xlabel('年龄', fontproperties=my_font)
plt.ylabel('身高(cm)', fontproperties=my_font)
# 设置图标标题
plt.title('男生女生在青春期身高的变化趋势对比图', fontproperties=my_font, size=25, color='blue')
# 制作图例
plt.legend(prop=my_font, loc='upper left')
# 制作栅格
plt.grid(alpha=0.4)
# 保存
plt.savefig('./tp05.png')
# 展示
plt.show()
效果如图:(不要点开看,我怕你的电脑屏幕装不下)
二、散点图
散点图很简单,和绘制折线图的差别就在于画图那一步。
# 导包
import matplotlib.pyplot as plt
import random
# x、y值的设定
x = range(1, 32)
y = [random.randint(25,42) for i in x]
# 设置画布的大小
plt.figure(figsize=(10, 5), dpi=80)
# 画图
plt.scatter(x, y)
# 绘制刻度
plt.xticks(x)
# 保存
plt.savefig('./tp06.png')
# 展示
plt.show()
效果如图:
三、条形图
# 导包
import matplotlib.pyplot as plt
from matplotlib import font_manager
from matplotlib.pyplot import MultipleLocator
# 设置x、y轴的值
a = ['1班', '2班', '3班', '4班', '5班', '6班']
b = [77, 69, 82, 70, 80, 88]
# 设置画布的大小
plt.figure(figsize=(10, 5), dpi=80)
# 设置文字,以支持中文显示
my_font = font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=5)
# 画图
rects = plt.bar(range(len(a)), [float(i) for i in b], width=0.3)
# 绘制坐标轴刻度
plt.xticks(range(len(a)), a, fontproperties=my_font, size=12)
plt.yticks(range(0, 100, 10), range(0, 100, 10))
# 在条形图上加标注
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2, height+0.3, str(height), ha='center')
# 保存图
plt.savefig('./tp07.png')
# 展示
plt.show()
与其折线图、散点图不同的地方:
值得注意的点:
另补充:绘制横向的条形图
# 导包
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 设置x、y轴的值
a = ['1班', '2班', '3班', '4班', '5班', '6班']
b = [77, 69, 82, 70, 80, 88]
# 设置画布的大小
plt.figure(figsize=(10, 5), dpi=80)
# 设置文字,以支持中文显示
my_font = font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=5)
# 画图
rects = plt.barh(range(len(a)), [float(i) for i in b], height=0.3)
# 绘制坐标轴刻度
plt.xticks(range(0,100,10), range(0,100,10))
plt.yticks(range(len(a)), a, fontproperties=my_font, size=12)
# 在条形图上加标注
for rect in rects:
width = rect.get_width()
plt.text(width, rect.get_y()+0.3/2, str(width), va='center')
# 保存图
plt.savefig('./tp11.png')
# 展示
plt.show()
四、直方图
# 导包
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 准备数据
time = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114,
119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99,
136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144,
105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123,
117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116,
108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125,
138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,
107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109,
106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123,
111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109,
141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103,
130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98,
117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111,
101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116,
111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
# 设置画布的大小
plt.figure(figsize=(16,8), dpi=80)
# 设置文字,以支持中文显示
my_font = font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=8)
# 设置组距
distance = 2
# 计算组数
group_num = int((max(time)-min(time)) / distance)
# 绘制直方图
plt.hist(time, bins=group_num)
# 绘制坐标轴刻度(x轴)
plt.xticks(range(min(time), max(time))[::2])
# 添加坐标轴标题
plt.xlabel('电影时长大小', fontproperties=my_font)
plt.ylabel('电影的数据量', fontproperties=my_font)
# 添加网格显示
plt.grid(alpha=0.5)
# 保存图
plt.savefig('./tp08.png')
# 展示图
plt.show()
绘制直方图的必要代码:
五、饼图
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 设置字体,以支持中文显示
my_font = font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=8)
# 各部分标签
label_list = ['第一部分', '第二部分', '第三部分']
# 各部分大小
size = [55, 35, 10]
# 各部分颜色
color = ['orange', 'limegreen', 'cornflowerblue']
# 各部分突出值
explode = [0, 0.05, 0]
# 画图
plt.pie(size,
explode=explode, # 各部分突出
colors=color, # 设置各部分颜色
labels=label_list, # 设置各部分标签
labeldistance=1.1, # 设置标签文本距离圆心位置, 1.1表示1.1倍半径
autopct="%1.1f%%", # 设置圆内文本信息(此处是占比信息)
shadow=False, # 设置阴影
startangle=90, # 起始角度,默认从0开始旋转
pctdistance=0.5) # 设置圆内文本距离圆心位置
# 设置横纵轴大小相等,这样的图才是圆的
plt.axis("equal")
# 设置饼图标题
plt.title('饼图Demo', fontproperties=my_font, size=20)
# 设置饼图图例
plt.legend(prop=my_font, loc=1, bbox_to_anchor=(1, 1))
# 用以辅助显示饼图中各部分的中文标签(试过font_manager显示无效,后续弄清楚了补充说明原因)
plt.rcParams['font.sans-serif']=['simsun'] #显示中文标签
# 保存图
plt.savefig('./tp09.png')
# 展示图
plt.show()