【问题标题】:Creating Bar Charts in Python HTML在 Python HTML 中创建条形图
【发布时间】:2013-01-20 16:54:51
【问题描述】:

寻找从字典创建条形图的系统有大约 150 行要放入图表。

{'test.com':十进制('40'),'cmi.com':十进制('70'),'dublin.ie':十进制('5'),'sfss.edu':十进制('35'), 'computer.ie': 十进制('10')}

一直在做的是放 for 循环并使其成为图像宽度,但不能像想要的那样动态地工作。

print "<img src='line.png' width='%d'>" % email[1]

任何帮助都会很棒。

【问题讨论】:

标签: python html


【解决方案1】:

结合

Creating Bar Charts in PythonGenerating a PNG with matplotlib when DISPLAY is undefined

能够解决这个感谢您的帮助。

#!/usr/bin/env python
print 'Content-type: text/html'
print.
import numpy as np
import matplotlib as mpl
mpl.use('Agg')

import matplotlib.pyplot as plt

谢谢

【讨论】:

    【解决方案2】:

    我正在使用的解决方案

    import matplotlib
    
    matplotlib.use('Agg')
    from pylab import *
    from decimal import *
    
    def main(my_dict):..
        bar_graph(my_dict, graph_title='Custoers')
        show_html()
    
    def bar_graph(name_value_dict, graph_title='', output_name='bargraph.png'):
        figure(figsize=(15, 10)) # image dimensions..
        title(graph_title, size='x-small')
    
        # add bars
        for i, key in zip(range(len(name_value_dict)), name_value_dict.keys()):
            barh(i, name_value_dict[key], color='red')
    
        # axis setup
        yticks(arange(0, len(name_value_dict)),
            [('%s: %d \n' % (name)) for name in
            zip(name_value_dict.keys(), name_value_dict.values())],
            size='small')
    
    # max_value = max(name_value_dict.values())
    #tick_range = arange(0, max_value, (max_value / len(ame_val_dict)))
    #formatter = FixedFormatter([str(x) for x in tick_range])
    # gca().yaxis.set_major_formatter(formatter)
    
        # gca().yaxis.grid(which='major')
        grid(True).
        savefig(output_name)
    
    def show_html():
        #print "<html><body>"
        print "<img src='bargraph.png'>"
        #print "</body></html>"
    
    if __name__ == "__main__":
        my_dict = {'la' : Decimal('20'), 'cmi' : Decimal(100)}
        main(my_dict)
    

    【讨论】:

      猜你喜欢
      • 2011-12-13
      • 2018-09-18
      • 2014-05-01
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-30
      相关资源
      最近更新 更多