matplotlib是Python的绘图库,实现数据的可视化。例如将数据表示为折线图、散点图等
绘制折线图/设置线条样式假设某地一天中,每隔2h气温分别是[15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15],单位oC,试绘制当天时间-气温折线图
from matplotlib import pyplot as plt temp = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15] time = range(2, 26, 2) # plot(x, y) x/y分别表示x轴/y轴的数据 # color参数: 设置线条的颜色(r红/g绿/b蓝y黄/k黑、或颜色单词、或十六进制RGB颜色代码) # linestyle参数:设置线条的样式(-实线、--虚线、-.点化线、:点虚线、''留空/空格/无线条) # linewidth参数:设置线条粗细 # alpha参数: 设置线条透明度 plt.plot(time, temp) plt.show()设置图片大小/像素、保存到本地
1)设置图片大小、像素;
2)保存到本地
from matplotlib import pyplot as plt
# figure():对图片进行全局配置,即之后所有图片都采用该配置
# figsize:图片大小(cm);
# dpi:清晰程度(像素)
# 20cmx8cm;80像素
fig = plt.figure(figsize=(20, 8), dpi=80)
temp = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
time = range(2, 26, 2)
plt.plot(time, temp)
# savefig(fig_path)
# 将图片保存到指定路径为指定名
# 可以保存为矢量图(*.svg),防止放大图片时失真
plt.savefig('./temperature.png')
plt.show()
设置坐标轴刻度
from matplotlib import pyplot as plt temp = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15] time = range(2, 26, 2) plt.plot(time, temp) # plt.xticks(list_xtick) # 设置x轴的刻度为list_xtick中的每个值 plt.xticks(range(1, 25)) # plt.yticks(list_ytick) # 设置y轴的刻度为list_ytick中的每个值 plt.yticks(range(int(min(temp)), int(max(temp))+1)) plt.show()设置坐标轴刻度为字符串、设置中文字体
1)设置坐标刻度为字符串
2)设置中文字体
假设10点10分到11点10分之间,每隔10分钟的温度分别为[21, 22, 22.3, 25, 25.3, 25.6, 26.2],单位oC。试绘制这段时间-气温折线图
matplotlib默认不显示中文,需要设置中文字体
# 查看系统中文字体 $ fc-list :lang=zh /usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light ......
from matplotlib import pyplot as plt
from matplotlib import font_manager
# font_manager.FontProperties
# fname传入字体路径
my_font = font_manager.FontProperties(fname='/usr/share/fonts/truetype/arphic/uming.ttc')
temp = [21, 22, 22.3, 25, 25.3, 25.6, 26.2]
time = range(10, 80, 10)
plt.plot(time, temp)
xtick_lables = ['{0}点{1}分'.format(numb//60+10, numb%60) for numb in time]
# 需要将原来的刻度(time),与新的刻度(xtick_lables)做一一对应
# rotation设置刻度旋转角度
# fontproperties设置字体
plt.xticks(time, xtick_lables, rotation=45, fontproperties=my_font)
plt.yticks(range(int(min(temp)), int(max(temp))+1))
plt.show()
设置描述信息
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname='/usr/share/fonts/truetype/arphic/uming.ttc')
temp = [21, 22, 22.3, 25, 25.3, 25.6, 26.2]
time = range(10, 80, 10)
plt.plot(time, temp)
xtick_lables = ['{0}点{1}分'.format(numb//60+10, numb%60) for numb in time]
plt.xticks(time, xtick_lables, rotation=45, fontproperties=my_font)
plt.yticks(range(int(min(temp)), int(max(temp))+1))
# xlabel/ylabel
# 设置x/y轴的标签
plt.xlabel('时间', fontproperties=my_font)
plt.ylabel('温度', fontproperties=my_font)
# title
# 设置图表的标题
plt.title('10点10分到11点10分每隔10分钟的温度变化情况', fontproperties=my_font)
plt.show()
绘制多条线、设置图例、绘制网格
1)绘制多条线
2)设置图例
3)绘制网格
假设下面是同学a、同学b从11岁到30岁每年交往的对象的个数
num_a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
num_b = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
绘制折线图表示两个人对象个数变化情况
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname='/usr/share/fonts/truetype/arphic/uming.ttc')
age = range(11, 31)
num_a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
num_b = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
# label参数设置图例标题
plt.plot(age, num_a, label='同学A')
plt.plot(age, num_b, label='同学B')
xtick_labels = ['{0}岁'.format(i) for i in age]
plt.xticks(age, xtick_labels, rotation=45, fontproperties=my_font)
plt.yticks(range(min(num_a+num_b), max(num_a+num_b)+1))
plt.xlabel('年龄(岁)', fontproperties=my_font)
plt.ylabel('个数(个)', fontproperties=my_font)
plt.title('11岁到30岁每年交往男女朋友的个数', fontproperties=my_font)
# grid()
# 绘制网格
plt.grid(alpha=0.2)
# legend()
# 使用图例
# prop参数设置图例使用字体
# loc参数设置图例显示位置
plt.legend(prop=my_font, loc='upper left')
plt.show()



