1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 12 python数据分析 matplotlib.pyplot绘制折线图

12 python数据分析 matplotlib.pyplot绘制折线图

时间:2021-05-28 05:22:12

相关推荐

12 python数据分析 matplotlib.pyplot绘制折线图

(一)数据分析

数据分析采取适当的方法对收集的大量数据进行分析,帮助人们做出判断并采取适当的行动。

数据分析的流程有:提出问题、准备数据、分析数据、得到结论、成果可视化(可选)

绘制折线图

(二) matplotlib.pyplot绘制折线图

1、基础

import matplotlib.pyplot as pltx=range(2,26,2)##x轴数据,是一个可迭代对象y=[i*2 for i in range(2,26,2)]##y轴数据,是一个可迭代对象plt.plot(x,y)###传入x、y绘制折线图plt.show()###在执行程序的时候展示图片

2、设置图片大小、刻度

import matplotlib.pyplot as pltx=range(2,26,2)y=[i*2 for i in range(2,26,2)]plt.figure(figsize=(20,8),dpi=80)###figsize设置图片大小,dpi设置像素大小越大越清晰plt.xticks(x)###设置x刻度,y轴同理plt.plot(x,y)###传入x、y绘制折线图plt.show()###在执行程序的时候展示图片

import matplotlib.pyplotimport matplotlib.pyplot as pltimport randomx=range(0,120)##x轴数据,是一个可迭代对象y=[random.randint(20,35) for i in range(120)]##y轴数据,是一个可迭代对象plt.figure(figsize=(20,8),dpi=80)###size设置图片大小dpi设置像素大小#xlabel=['hellow{0}'.format(i) for i in x ]xlabel=['10点{0}分'.format(i) for i in range(60) ]xlabel.extend(['11点{0}分'.format(i) for i in range(60) ])####将数字的刻度x与字符串的刻度xlabel对应,两者必须等长####rotation表示刻度旋转度数plt.xticks(x[::10],xlabel[::10],rotation=90)plt.xlabel('添加x轴标签')plt.ylabel('添加y轴标签')plt.title('添加标题')plt.plot(x,y)plt.show()

3、设置显示中文字体

import matplotlib.pyplotimport matplotlib.pyplot as pltimport randomx=range(0,120)y=[random.randint(20,35) for i in range(120)]plt.figure(figsize=(20,8),dpi=80)##显示中文设置1font = {'family': 'Microsoft YaHei','weight': 'bold'}matplotlib.rc('font',**font)#xlabel=['hellow{0}'.format(i) for i in x ]xlabel=['10点{0}分'.format(i) for i in range(60) ]xlabel.extend(['11点{0}分'.format(i) for i in range(60) ])plt.xticks(x[::10],xlabel[::10],rotation=90)plt.xlabel('添加x轴标签')plt.ylabel('添加y轴标签')plt.title('添加标题')plt.plot(x,y)plt.show()

import matplotlib.pyplotimport matplotlib.pyplot as pltimport randomfrom matplotlib import font_managerx=range(0,120)y=[random.randint(20,35) for i in range(120)]plt.figure(figsize=(20,8),dpi=80)#xlabel=['hellow{0}'.format(i) for i in x ]xlabel=['10点{0}分'.format(i) for i in range(60) ]xlabel.extend(['11点{0}分'.format(i) for i in range(60) ])##显示中文设置2:fname为本机字体文件所在的位置my_font=font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')plt.xticks(x[::10],xlabel[::10],rotation=90,fontproperties=my_font)plt.xlabel('添加x轴标签',fontproperties=my_font)plt.ylabel('添加y轴标签',fontproperties=my_font)plt.title('添加标题',fontproperties=my_font)plt.plot(x,y)plt.show()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。