为什么我在绘制折线图时出错
Why I am getting an error with plotly line chart
我是 Plotly 的新手,正在尝试使用 Plotly 折线图绘制以下枢轴 table。我的代码如下。错误也附在这里。
请问我错在哪里
# create traces
trace0 = go.Scatter(
x = df_50_08_16_PVT['Device_ID'],
y = df_50_08_16_PVT['NoiseLevel[dB]'],
mode = 'lines+markers',
name = 'Noise Level'
)
trace1 = go.Scatter(
x = df_50_08_16_PVT['Device_ID'],
y = df_50_08_16_PVT['SPEC_MAX'],
mode = 'lines+markers',
name = 'SPEC_MAX'
)
data = [trace0, trace1] # assign traces to data
layout = go.Layout(
title = 'Line chart showing three different modes'
)
fig = go.Figure(data=data,layout=layout)
pyo.plot(fig, filename='line1.html')
正如 Rob 所提到的,我使用了 Index.get_level_values(level) 并且图表即将出现,但不是以预期的方式。请在下面查看我获得的和预期的图表
预计
- 你没有提供样本数据所以我模拟了
- 您最初的错误是一个直接的 pandas 编码错误。您需要使用 get_level_values
来引用索引中的值
- 由于多个 y 值共享相同的 x 轴值
,您的图表在直接切换后 spikey
- 仅使用索引位置 (
reset_index().index
) 解决了这个问题
- 现在需要构建 x 轴以包含 Device_ID
- 终于修复了hovertemplate
import random
import numpy as np
import pandas as pd
import plotly.graph_objects as go
# question has no data, generate some...
df_50_08_16_PVT = pd.concat(
[
pd.DataFrame(
index=pd.MultiIndex.from_product(
[
[f"{h}_{n}" for n in np.random.randint(1000, 2000, 4)],
list("abcdefghijklmnop")[0 : random.randint(4, 15)],
],
names=["Device_ID", "filler"],
)
)
for h in ["SF", "TT"]
]
).pipe(
lambda d: d.sort_index().assign(
**{
"NoiseLevel[dB]": np.random.uniform(-97, -102, len(d)),
"SPEC_MAX": np.random.uniform(-96.5, -97, len(d)),
}
)
)
# build array has first instance of Device_ID and every other value empty string
s = (
df_50_08_16_PVT.index.get_level_values("Device_ID")
.to_series()
.groupby("Device_ID")
.cumcount()
)
ticks = np.where(s == 0, s.index, "")
fig = go.Figure(
data=[
go.Scatter(
x=df_50_08_16_PVT.reset_index().index,
y=df_50_08_16_PVT[c],
mode="lines+markers",
meta=df_50_08_16_PVT.index.get_level_values("Device_ID"),
name=c,
hovertemplate="(%{meta},%{y})",
)
for c in df_50_08_16_PVT.columns
],
layout={
"title": "Line chart showing three different modes",
"xaxis": {
"ticktext": ticks,
"tickmode": "array",
"tickvals": df_50_08_16_PVT.reset_index().index,
},
},
)
fig
我是 Plotly 的新手,正在尝试使用 Plotly 折线图绘制以下枢轴 table。我的代码如下。错误也附在这里。
请问我错在哪里
# create traces
trace0 = go.Scatter(
x = df_50_08_16_PVT['Device_ID'],
y = df_50_08_16_PVT['NoiseLevel[dB]'],
mode = 'lines+markers',
name = 'Noise Level'
)
trace1 = go.Scatter(
x = df_50_08_16_PVT['Device_ID'],
y = df_50_08_16_PVT['SPEC_MAX'],
mode = 'lines+markers',
name = 'SPEC_MAX'
)
data = [trace0, trace1] # assign traces to data
layout = go.Layout(
title = 'Line chart showing three different modes'
)
fig = go.Figure(data=data,layout=layout)
pyo.plot(fig, filename='line1.html')
正如 Rob 所提到的,我使用了 Index.get_level_values(level) 并且图表即将出现,但不是以预期的方式。请在下面查看我获得的和预期的图表
预计
- 你没有提供样本数据所以我模拟了
- 您最初的错误是一个直接的 pandas 编码错误。您需要使用 get_level_values 来引用索引中的值
- 由于多个 y 值共享相同的 x 轴值 ,您的图表在直接切换后 spikey
- 仅使用索引位置 (
reset_index().index
) 解决了这个问题
- 现在需要构建 x 轴以包含 Device_ID
- 终于修复了hovertemplate
import random
import numpy as np
import pandas as pd
import plotly.graph_objects as go
# question has no data, generate some...
df_50_08_16_PVT = pd.concat(
[
pd.DataFrame(
index=pd.MultiIndex.from_product(
[
[f"{h}_{n}" for n in np.random.randint(1000, 2000, 4)],
list("abcdefghijklmnop")[0 : random.randint(4, 15)],
],
names=["Device_ID", "filler"],
)
)
for h in ["SF", "TT"]
]
).pipe(
lambda d: d.sort_index().assign(
**{
"NoiseLevel[dB]": np.random.uniform(-97, -102, len(d)),
"SPEC_MAX": np.random.uniform(-96.5, -97, len(d)),
}
)
)
# build array has first instance of Device_ID and every other value empty string
s = (
df_50_08_16_PVT.index.get_level_values("Device_ID")
.to_series()
.groupby("Device_ID")
.cumcount()
)
ticks = np.where(s == 0, s.index, "")
fig = go.Figure(
data=[
go.Scatter(
x=df_50_08_16_PVT.reset_index().index,
y=df_50_08_16_PVT[c],
mode="lines+markers",
meta=df_50_08_16_PVT.index.get_level_values("Device_ID"),
name=c,
hovertemplate="(%{meta},%{y})",
)
for c in df_50_08_16_PVT.columns
],
layout={
"title": "Line chart showing three different modes",
"xaxis": {
"ticktext": ticks,
"tickmode": "array",
"tickvals": df_50_08_16_PVT.reset_index().index,
},
},
)
fig