我正在测量设备。我采样了不同的值,比如不同时间的温度。现在我喜欢通过 plotly 和 pandas 将我的数据可视化为曲线。
我正在寻找如何做到这一点的建议。
举个例子并为简单起见,我将 2 个数据集存储在 2 个单独的 csv 文件中,具有相同的标题。喜欢:ds1.csv
timestamp value
2021-09-09 16:47:36.231446 355
2021-09-09 16:47:41.208629 355
ds2.csv
timestamp value
2021-09-09 16:47:36.236132 30
2021-09-09 16:47:46.235707 30
2021-09-09 16:47:56.240340 30
值的时间戳不相同,但它们指的是同一个时钟。
所以我喜欢用plotly把ds1和ds2画成同一个图。
fig1 = plotly.express.line(ds1, x = 'timestamp', y = 'value', title='Temp1')
fig2 = plotly.express.line(ds2, x = 'timestamp', y = 'value', title='Temp2')
?fig = fig1 and fig2?
fig.show()
知道如何做到这一点吗?
将文件作为熊猫数据帧读取。如果使用 plotly 图形对象,在同一个图上绘制两个图形会更容易go.Bar or go.Scatter
。您可以通过提供范围和dticks
参数使 xaxis(timestamps) 刻度线性变化。
下面的示例显示了一个简单的散点图,其中包含绘制在同一图形上的两个数字。
import numpy as np
import pandas as pd
import plotly.offline as py
import plotly.graph_objs as go
from plotly import tools
df1 = pd.DataFrame({"X":np.linspace(0,30,10), "Y":np.random.rand(10)})
df2 = pd.DataFrame({"A":np.linspace(0,40,10), "B":np.random.rand(10)})
# plotting the graphs...
def scatter_chart(x, y, color, name):
trace = go.Scatter(
x=x.values,
y=y.values,
name=name,
marker=dict(
color=color,
line=dict(
color=color,
width=1)
),
)
return trace
# for loop could be used
trace1 = scatter_chart(df2["A"], df2["B"], 'rgb(128, 0, 128)', "df2")
trace2 = scatter_chart(df1["X"], df1["Y"], 'rgba(50, 171, 96, 0.6)', "df1")
fig = tools.make_subplots(rows=1,cols=1, vertical_spacing=0.5)
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.update_layout(
title="df2 and df1 plot",
height=600,
width=600,
# annotations=annotations,
xaxis=dict(tickangle=-45),
legend=dict(x=0.029, y=1.038, font_size=10),
margin=dict(l=100, r=20, t=70, b=70),
paper_bgcolor='rgb(248, 248, 255)',
plot_bgcolor='rgb(248, 248, 255)',)
# xaxis could be customized further
fig['layout']['xaxis'].update(title='', range=[0, 500], dtick=50, autorange=False)
py.iplot(fig, filename='pageviews_plots_4')
请运行代码并查看...希望你能明白。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句