2016-02-14 43 views
0

我是新來劇情和工作的腳本生成基於從數據庫中拉出的一些結果的圖形。但是,當我將數據發送到繪圖時,只有三條曲線中的每條曲線的第一個數據點正在繪圖。我已經驗證了這些列表包含正確的數據,我甚至只是簡單地粘貼列表,而不是動態地創建變量。不幸的是,每次只有第一個數據點被繪製。有人知道我在這裏失蹤了嗎?如果需要,我也向另一個圖書館開放。Pylotly for python,只有第一個數據點正在繪圖

也可以讓x軸顯示爲一個字符串嗎?

import plotly.plotly as py 
import plotly.graph_objs as go 
# Custom database class, works fine. 
from classes.database import DatabaseConnection 

# Database Connections and instances 
db_instance = DatabaseConnection() 
db_conn = db_instance.conn 
db_cur = db_instance.cur 



def main(): 


    # Get a list of versions and their stats. 
    db_cur.execute(
     """ 
     select row_to_json(x) from 
     (SELECT 
       versions.version_number, 
       cast(AVG(results.average) as double precision) as average, 
       cast(AVG(results.minimum) as double precision) as minimum, 
       cast(AVG(results.maximum) as double precision) as maximum 
        FROM versions,results 
        WHERE 
         versions.version_number = results.version_number 
         GROUP BY 
         versions.version_number) x; 
       """ 
    ) 

    versions = [] 
    average = [] 
    minimum = [] 
    maximum = [] 

    unclean = db_cur.fetchall() 

    # Create lists for x and y coordinates. 
    for row in unclean: 
     versions.append(row[0]['version_number']) 
     average.append(int(row[0]['average'])) 
     minimum.append(int(row[0]['minimum'])) 
     maximum.append(int(row[0]['maximum'])) 

    grph_average = go.Scatter(
     x=versions, 
     y=average, 
     name = 'Average', 
     mode='lines', 
    ) 

    grph_minimum = go.Scatter(
     x=versions, 
     y=minimum, 
     name = 'Minimum', 
     mode='lines', 
    ) 

    grph_maximum = go.Scatter(
     x=versions, 
     y=maximum, 
     name = 'Maximum', 
     mode='lines', 
    ) 


    data = go.Data([grph_average, grph_minimum, grph_maximum]) 
    # Edit the layout 
    layout = dict(title = 'Responses', 
       xaxis = dict(title = 'Versions'), 
       yaxis = dict(title = 'Ms'), 
      ) 

    fig = dict(data=data, layout=layout) 
    py.plot(fig, filename='response-times', auto_open=False) 


if __name__ == '__main__': 
    main() 

該查詢返回的數據如下,如果你想在值堵塞:

versions = ['6.1', '5.0', '5.2'] 
average = [11232, 29391, 10429] 
minimum = [3641, 7729, 3483] 
maximum = [57440, 62535, 45201] 

回答

0

它看起來像是我的x軸的問題。通過在版本號之前添加一些文本,並特別將類型轉換爲字符串,我可以正確地生成圖形。

# Create lists for x and y coordinates. 
for row in unclean: 
    versions.append("Version: " + str(row[0]['version_number'])) 
    average.append(int(row[0]['average'])) 
    minimum.append(int(row[0]['minimum'])) 
    maximum.append(int(row[0]['maximum'])) 
0

下面是一些matplotlib可能讓你開始這個是:

import matplotlib.pyplot as plt 

versions = ['6.1', '5.0', '5.2'] 
average = [11232, 29391, 10429] 
minimum = [3641, 7729, 3483] 
maximum = [57440, 62535, 45201] 

plt.plot(minimum) 
plt.plot(average) 
plt.plot(maximum) 
plt.xticks(range(len(versions)), versions) 
相關問題