美文网首页Python之路程序员我的Python自学之路
新手向——制作web图表(基于Python和GooPyChart

新手向——制作web图表(基于Python和GooPyChart

作者: treelake | 来源:发表于2016-10-27 14:10 被阅读3315次

    Creating Graphs with Python and GooPyCharts

    • 如果你需要一个简单、美观、易用的可嵌入网页的web可交互图表(可放大缩小),并且可以保存为PNGHTML,数据可导出CSV,那就是它了。

    • GooPyCharts是对于谷歌图表Google Charts API)的python封装。GooPyCharts的语法类似于MATLAB,实际上是对matplotlib库的替代。

    • 安装 pip install gpcharts pip install future


    第一张图

    • 三行就是一张简单的图:以默认的从0开始间隔1的x坐标画出你给出的数组(plot函数里的列表),自动做拟合。
    from gpcharts import figure
    my_plot = figure(title='Demo')
    my_plot.plot([1, 2, 10, 15, 12, 23])
    

    运行后你的默认浏览器会打开并展示如下:


    画条形图

    • 我们画出三个不同地区的气温-日期条形图。日期自动转换为Apr 1这种形式。
    from gpcharts import figure
    # 获取图像对象并设置x,y轴的值
    fig3 = figure()
    xVals = ['Temps','2016-03-20','2016-03-21','2016-03-25','2016-04-01']
    yVals = [['Shakuras','Korhal','Aiur'],[10,30,40],[12,28,41],[15,34,38],[8,33,47]]
    # 添加标题和Y轴标注,画条形图
    fig3.title = 'Weather over Days'
    fig3.ylabel = 'Dates'
    fig3.bar(xVals, yVals)
    

    画散点图

    • 将上面的代码稍作修改可以得到散点图
    from gpcharts import figure
    #
    my_fig = figure()
    xVals = ['Dates','2016-03-20','2016-03-21','2016-03-25','2016-04-01'] # 第一个元素与上面不同
    yVals = [['Shakuras','Korhal','Aiur'],[10,30,40],[12,28,41],[15,34,38],[8,33,47]]
    #
    my_fig.title = 'Scatter Plot'
    my_fig.ylabel = 'Temps' # y轴标注做了修改
    #
    my_fig.scatter(xVals, yVals)
    

    柱状图

    • 很简单的柱状图
    from gpcharts import figure
    #
    my_fig = figure()
    my_fig.title = 'Random Histrogram'
    my_fig.xlabel = 'Random Values'
    vals = [10, 40, 30, 50, 80, 100, 65]
    my_fig.hist(vals)
    

    其它

    相关文章

      网友评论

      本文标题:新手向——制作web图表(基于Python和GooPyChart

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