我正在尝试获取我的influxdb测量值的图形。要显示图形,我使用plot.ly和python。我从python开始以sql请求从localhost:8086获取json数据,但是它不起作用。我已经尝试了一些方法,向您发送了我的一小段代码:
import plotly
import plotly.plotly as py
import numpy as np
import plotly.graph_objs as go
import plotly.offline as ply
import pandas as pd
from plotly.tools import FigureFactory as FF
if __name__ == "__main__":
df = pd.read_json('http://localhost:8086/query?q=SELECT%20%22I%22%20FROM%20%22michelin%22.%22autogen%22.%22mqtt_consumer%22%20WHERE%20time%20%3E%20now()%20-%202d%20AND%20%22topic%22=%27PI1%27')
print(df['results'])
#df = [go.Scatter(x=df['time'], y=df['mqtt_consumer.mean_I'])]
ply.plot(df, filename = 'time-series-simple')
带有请求的链接向我显示了此内容:
我想要一个时间序列,其值“ I”是时间的函数。 如果有人可以帮助我,谢谢您的反馈
答案 0 :(得分:0)
在绘制之前必须先松开所有键。您要绘制的json对象内部的数据位于最内层的嵌套级别。您需要达到目标。试试这个
dfff = pd.DataFrame(df["results"][0]["series"][0]["values"]).T
#dfff.plot()
特别是如果要使用plotly
进行绘制,则:
import plotly.plotly as py
import pandas as pd
import numpy as np
dfff = pd.DataFrame(df["results"][0]["series"][0]["values"]).T
dfff.columns = ["timestamp","value"]
py.iplot([{
'x': dfff.timestamp,
'y': dfff["value"],
'name': "value"
} ], filename='yourfilename')
答案 1 :(得分:0)
要通过Plot.ly绘制数据,您需要先创建一条轨迹,以定义X,Y轴数据。
import plotly.plotly as py
import plotly.graph_objs as go
trace = go.Scatter(
x = ("Your X data, Must be a numpy array"),
y = ("Your Y data, Must be a numpy array"),
mode = 'markers'
)
data = [trace]
然后,您可以简单地
py.plot(data, filename='basic-line')