这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » python开发中用matplotlib画折线图、柱状图、散点图代码详解

共3条 1/1 1 跳转至

python开发中用matplotlib画折线图、柱状图、散点图代码详解

助工
2020-11-25 10:57:34     打赏

今天我们举例,有两组数据,一组数据是dgp一组是对应的年份。那么我们可以画出年份与GDP的关系图,是上升还是下降来来来看代码。

#-*-coding:utf-8-*-

#**********************************

#**http://weibo.com/lixiaodaoaaa#

#**createat2017/5/2020:55***

#******by:lixiaodaoaaa***********

frommatplotlib.font_managerimportFontManager,FontProperties

importsubprocess

importmatplotlib.pyplotasplot

defgetChineseFont():

returnFontProperties(fname='/System/Library/Fonts/PingFang.ttc')

if__name__=='__main__':

years=[2010,2011,2012,2013,2014,2015,2016,2017]

gdps=[256,289,302,356,389,400,402,436]

plot.ylabel('gdp指标')

plot.xlabel("年份")

plot.plot(years,gdps,color='red',marker='o',linestyle='solid')

plot.title('年份与GDP的关系图',fontproperties=getChineseFont())

plot.show()

效果图如下:


同样的我们可以画一个柱状图:

#-*-coding:utf-8-*-

#**********************************

#**http://weibo.com/lixiaodaoaaa#

#**createat2017/5/2020:55***

#******by:lixiaodaoaaa***********

frommatplotlib.font_managerimportFontManager,FontProperties

importsubprocess

importmatplotlib.pyplotasplot

defgetChineseFont():

returnFontProperties(fname='/System/Library/Fonts/PingFang.ttc')

if__name__=='__main__':

years=[2010,2011,2012,2013,2014,2015,2016,2017]

gdps=[256,289,302,356,389,400,402,436]

plot.ylabel('gdp指标',fontproperties=getChineseFont())

plot.xlabel("年份",fontproperties=getChineseFont())

#plot.plot(years,gdps,color='red',marker='o',linestyle='solid')

plot.title('年份与GDP的关系图',fontproperties=getChineseFont())

plot.bar(years,gdps)

plot.show()

效果图如下


散点图

plot.scatter(years,gdps)



以上就是关于扣丁学堂Python培训之用matplotlib画折线图、柱状图、散点图的详细介绍,最后想要了解更多关于Python发展前景趋势,请关注扣丁学堂python培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供最新的Python视频教程系统,通过千锋扣丁学堂金牌讲师在线录制的Python视频教程课程,让你快速掌握Python从入门到精通开发实战技能。扣丁学堂Python技术交流群:816572891。



工程师
2020-11-26 21:29:12     打赏
2楼

代码写的很不错的


工程师
2020-11-27 22:24:57     打赏
3楼

学习一下


共3条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]