美文网首页
python中用matplotlib画折线图、柱状图、散点图

python中用matplotlib画折线图、柱状图、散点图

作者: NoValue | 来源:发表于2017-05-21 13:31 被阅读4958次

先看画折线图 观察两组数据的关联关系

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

# -*- coding:utf-8 -*-
# **********************************
# ** http://weibo.com/lixiaodaoaaa #
# ** create at 2017/5/20   20:55 ***
# ****** by:lixiaodaoaaa ***********

from matplotlib.font_manager import FontManager, FontProperties
import subprocess
import matplotlib.pyplot as plot

def getChineseFont():
    return FontProperties(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()

效果图如下:

plat01.png

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

# -*- coding:utf-8 -*-
# **********************************
# ** http://weibo.com/lixiaodaoaaa #
# ** create at 2017/5/20   20:55 ***
# ****** by:lixiaodaoaaa ***********

from matplotlib.font_manager import FontManager, FontProperties
import subprocess
import matplotlib.pyplot as plot


def getChineseFont():
    return FontProperties(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()

效果图如下

plat02.png

散点图呢?

  plot.scatter(years,gdps)

相关文章

网友评论

      本文标题:python中用matplotlib画折线图、柱状图、散点图

      本文链接:https://www.haomeiwen.com/subject/mcnmxxtx.html