Graphing in Python 3.x

后端 未结 9 785
借酒劲吻你
借酒劲吻你 2021-02-02 15:04

In Python 2.6, I used matplotlib to make some simple graphs. However, it is incompatible with Python 3.1.

What are some alternative modules that can accomplish the same

9条回答
  •  别那么骄傲
    2021-02-02 15:31

    I wrote a small example that runs in python 3 and uses the google chart api (as suggested by Duncan, I wrote the solution after seeing this post).

    It is not ideal since it adds a dependency one a 3rd party that might break backward compatibility at any time, but the graphs are nice and there is absolutely no added dependency on the python code. Worth considering for not 'mission critical code'.

    You can find/download the example here. Here is the graph that it generates from data in a .xml file: alt text

    # build the query with template parameters
    query ="http://chart.apis.google.com/chart?chxl=0:__X_LABELS__&chxp=__X_LABELS_POS__&chxr=0,__MIN_TIME__,__MAX_TIME__|1,__MIN_WEIGHT__,__MAX_WEIGHT__&chxs=0,676767,11.5,0,lt,676767|1,676767,11.5,0,lt,676767&chxt=x,y&chs=800x300&cht=lc&chco=3072F3&chds=__MIN_WEIGHT__,__MAX_WEIGHT__&chd=t:__COMMASEP_WEIGHT__&chdl=Weight&chdlp=b&chls=2,4,1&chma=5,5,5,25&chtt=Your+Weight+Timeline"
    
    [...]
    
    # relace template with data
    query = query.replace('__X_LABELS__', strXLabels)
    query = query.replace('__X_LABELS_POS__', strXLabelsPos)
    query = query.replace('__MIN_TIME__', str(min(lst_dateEpoch)))
    query = query.replace('__MAX_TIME__', str(max(lst_dateEpoch)))
    
    [...]
    
    # use 'urllib.request' to download the data & write to file
    sock = urllib.request.urlopen(query)
    image_bytes = sock.read()
    sock.close()
    
    fh = open('Weight_GoogleGraphApi.png', 'wb')
    fh.write(image_bytes)
    fh.close()
    

提交回复
热议问题