安装
pip install matplotlib
图形的基础绘制
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(6, 3), dpi=80)
x = range(2, 26, 2)
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
plt.xticks(x)
_ytick_lables = [i / 2 for i in range(0, 60)]
plt.yticks(_ytick_lables[::3])
plt.plot(x, y)
plt.savefig('t1.svg')
plt.show()
折线图
import random
from matplotlib import font_manager
from matplotlib import pyplot
x = range(120)
a = [random.randint(18, 30) for i in range(120)]
_x = list(x)
_xtick_lables = ['10:{}'.format(i) for i in range(60)]
_xtick_lables += ['11点{}分'.format(i) for i in range(60)]
pyplot.xlabel('时间', fontproperties=my_font, size='15')
pyplot.ylabel('温度 单位(℃)', fontproperties=my_font, size='15')
pyplot.title('10点到12点的温度变化', fontproperties=my_font, size='20', color='red')
pyplot.xticks(_x[::5], _xtick_lables[::5], rotation=45, fontproperties=my_font)
pyplot.plot(x, a)
pyplot.show()
绘制多条折线图
from matplotlib import pyplot, font_manager
my_font = font_manager.FontProperties(fname='甜心音乐艺术.ttf')
man = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 4, 3, 3, 1, 1, 1]
woman = [1, 0, 3, 1, 2, 3, 3, 2, 1, 2, 3, 4, 5, 6, 7, 2, 2, 2, 1]
x = range(11, 30)
pyplot.xlabel('人数', fontproperties=my_font)
pyplot.ylabel('年龄', fontproperties=my_font)
pyplot.title('年龄与交朋友的数量关系', fontproperties=my_font, size='20', color='blue')
pyplot.grid(alpha=0.3, linestyle='--')
_x = x
_xticks_label = ['{}岁'.format(i) for i in range(11, 30)]
pyplot.xticks(_x, _xticks_label, fontproperties=my_font, rotation=30)
pyplot.plot(x, man, label='男人', linewidth=2)
pyplot.plot(x, woman, label='女人', color='cyan', linestyle='-.')
pyplot.legend(prop=my_font, loc=2)
pyplot.text(25, 7, '女最高', fontdict={'size': '24', 'color': 'b','fontproperties':my_font})
pyplot.text(x=15,
y=3,
s="msadad",
fontsize=50,
color="gray",
alpha=0.4,
)
pyplot.show()
条形图
from matplotlib import font_manager
from matplotlib import pyplot as plt
a = ['战狼2', '哪吒之魔童降世', '流浪地球', '复仇者联盟4:终局之战', '红海行动', '美人鱼', '唐人街探案2',
'我和我的祖国', '我不是药神', '中国机长', '速度与激情8', '西虹市首富', '速度与激情7', '捉妖记',
'复仇者联盟:无限战争', '捉妖记2', '羞羞的铁拳', '疯狂的外星人', '海王', '变形金刚4:绝迹重生']
b = [56.39, 49.34, 46.18, 42.05, 36.22, 33.9, 33.71, 31.46, 30.75, 28.84,
26.49, 25.27, 24.26, 24.21, 23.7, 22.19, 21.9, 21.83, 19.97, 19.79]
fig = plt.figure(figsize=(15, 7), dpi=80)
plt.grid(alpha=0.3)
my_font = font_manager.FontProperties(fname='皇冠字体.ttf')
plt.title('2020年5月20日电影房排行', fontproperties=my_font, size=20, color='orange')
plt.xlabel('票房:(单位:亿)', fontproperties=my_font)
plt.yticks(range(len(a)), a, fontproperties=my_font)
plt.barh(a, b, height=0.755, color='orange')
plt.show()
绘制多条条形图
from matplotlib import font_manager
from matplotlib import pyplot as plt
a = ['猩猿崛起3:终极之战', '敦刻尔克', '蜘蛛侠:英雄归来', '战狼2']
b_16 = [15746, 312, 4497, 319]
b_15 = [12357, 156, 2045, 168]
b_14 = [2358, 399, 2358, 362]
my_font = font_manager.FontProperties(fname='皇冠字体.ttf')
bar_width = 0.2
plt.ylabel('票房(单位:万元)', fontproperties=my_font)
plt.title('2019年9月14——15日票房', fontproperties=my_font, size='20', color='blue')
# 这是x轴刻度
x_14 = list(range(len(a)))
x_15 = [i + bar_width for i in x_14]
x_16 = [i + bar_width * 2 for i in x_14]
plt.xticks(x_15, a, fontproperties=my_font)
# 绘制条形图
plt.bar(x_14, b_14, width=bar_width,label='14日')
plt.bar(x_15, b_15, width=bar_width,label='15日')
plt.bar(x_16, b_16, width=bar_width,label='16日')
# 添加图例
plt.legend(prop=my_font)
plt.show()
散点图
from matplotlib import font_manager
from matplotlib import pyplot as plt
sanyue = [10,9,10,12,8,13,13,8,5,10,12,
10,10,12,12,11,13,16,16,16,13,19,
15,16,20,8,12,7 ,15,9,13]
siyue = [10,4,4,10,9,12,10,15,15,14,
10,15,14,21,22,18,17,19,20,15,
11,15,14,16,19,22,23,23,25,27]
fig = plt.figure(figsize=(15, 5), dpi=80)
plt.grid(alpha=0.3)
my_font = font_manager.FontProperties(fname='甜心音乐艺术.ttf')
plt.title('2020年3-4月最高温度(静宁)', fontproperties=my_font, size=20, color='#7EC0EE')
plt.ylabel('温度:(单位:℃)', fontproperties=my_font)
x_3 = range(1, 32)
x_4 = range(41, 71)
_x = list(x_3) + list(x_4)
_xticks_labels = ['3月{}日'.format(i) for i in x_3]
_xticks_labels += ['4月{}日'.format(i - 40) for i in x_4]
plt.xticks(_x[::2], _xticks_labels[::2], fontproperties=my_font, rotation=90)
plt.scatter(x_3, sanyue,label='3月份')
plt.scatter(x_4, siyue,label='4月份')
plt.legend(prop=my_font,loc=9)
plt.show()
直方图
import random
from matplotlib import font_manager
from matplotlib import pyplot as plt
my_font = font_manager.FontProperties(fname='华康少女字体完整版(1).ttf')
plt.title('电影时长频率次数', fontproperties=my_font, size='20', color='blue')
random.seed(1)
a = [random.randint(40, 150) for i in range(250)]
# print(a)
plt.grid()
# 计算组距
d = 5
num_bins = int((max(a) - min(a)) // d)
print(num_bins)
plt.xticks(range(min(a), max(a) + d, d))
plt.hist(a, num_bins, density=True) # 显示出现的次数频率
plt.show()
饼状图
import xlrd
from matplotlib import pyplot as plt
data = xlrd.open_workbook('C:\\Users\\gelan\\Desktop\\student.xlsx')
print(data)
table = data.sheets()[0]
print(table)
print(table.nrows)
print(table.ncols)
print(table.row_values(3))
print(type(table.col_values(1)))
name = table.col_values(0)
name.pop(0)
cj = table.col_values(1)
cj.pop(0)
print(name)
plt.rcParams['font.sans-serif'] = ['simhei']
plt.xticks(range(len(name)), name, )
explode = [0, 0.1, 0, 0]
plt.pie(labels=name, x=cj, autopct='%.0f%%', explode=explode, shadow=True)
plt.show()