今天我们举例,有两组数据,一组数据是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。