用图表示,不显示与np.array数据集的坐标

时间:2017-08-29 20:37:48

标签: python numpy ipython data-visualization plotly

我希望显示一个1000浮动的数据集,我已经决定用剧情做这个,我想离线做,我遇到了一个我真的无法理解的问题 - 我根本就不知道我做错了什么。

让我们跳转到代码。首先,我将通过一个小的np.array

显示代码应该可以工作
import numpy as np
import plotly as py
import plotly.graph_objs as go

list = [1.2,2.3,3.3,4.4,5.4,6.4] 
x_data = np.array(list)
y_data = np.array(list)

#x_data = np.array(graph_test_q())
#y_data = np.array(graph_test_h())

trace = go.Scatter(
   x = x_data,
   y = y_data,
)

data = [trace]
fig = dict(data=data)
py.offline.plot(fig, filename='hejsa.html')
print data

以上代码的输出: Output of small np.array 似乎工作正常,但是: 下面是代码,我使用从函数创建的np.array,从postgrSQL db中提取它。我已经检查了,它确实在终端中打印了数据。

def graph_test_q():
    conn = psycopg2.connect("dbname='database1' user='postgres'        password='FFgg1905560' host='localhost' port='5432'")
    cur  = conn.cursor()
    cur.execute("SELECT q FROM pump_data_test WHERE pump_id = 1229")
    rows = cur.fetchall()
    conn.commit()
    conn.close()
    return rows

def graph_test_h():
    conn = psycopg2.connect("dbname='database1' user='postgres'    password='FFgg1905560' host='localhost' port='5432'")
    cur  = conn.cursor()
    cur.execute("SELECT h FROM pump_data_test WHERE pump_id = 1229")
    rows = cur.fetchall()
    conn.commit()
    conn.close()
    return rows

#list = [1.2,2.3,3.3,4.4,5.4,6.4]
#x_data = np.array(list)
#y_data = np.array(list)

x_data = np.array(graph_test_q())
y_data = np.array(graph_test_h())

trace = go.Scatter(
    x = x_data,
    y = y_data,
)

data = [trace]
fig = dict(data=data)
py.offline.plot(fig, filename='hejsa.html')
print data

现在我觉得这很奇怪,这些新np.arrays的输出是这个空图: enter image description here

当我点击右下角的链接 - “export to plot.ly”时,这就是我得到的输出: enter image description here 在这里,我可以看到左侧的图形,就像它应该是的那样。如果有人能帮助我找出我做错了什么,我将非常感激。

编辑: 来自评论:显示x_data&的dtypes的代码y_data(x_data = np.array(graph_test_q())& y_data = np.array(graph_test_h())):

print(x_data.dtype)
print(y_data.dtype)

输出:

float64

float64

0:96:执行错误:“file://hejsa.html”无法理解“打开位置”消息。 (-1708)

70:78:执行错误:无法获取应用程序“firefox”。 (-1728)  *在http://127.0.0.1:5000/上运行(按CTRL + C退出)

1 个答案:

答案 0 :(得分:0)

, Comma operator As a binary operator, the comma creates an array. As a unary operator, the comma creates an array with one member. Place the comma before the member. 会返回元组,因此最终会得到一个fetchall数组,其形状为Numpy,其中n是结果数。您可以使用以下索引以(n, 1)的正确格式获取数据:

Plotly

请参阅下面的完整演示。

np.array(graph_test_q())[:,0]

enter image description here