[英]Plotly Express - plot subset of dataframe columns by default and the rest as option
I am using plotly express to plot figures this way:我正在使用 plotly 快递到 plot 这样的数字:
fig = px.line(df,
x=df.index,
y=df.columns
)
It displays the graph properly and shows all the columns by default (as lines in the graph) with option to uncheck (or check) them to disable showing whatever we want if needed.它正确显示图表并默认显示所有列(作为图表中的线),并可选择取消选中(或选中)它们以禁用显示我们想要的任何内容(如果需要)。 What I would like is to show the same graph but by default uncheking some of the columns initially and keep the option to check or uncheck them for visualization.
我想要显示相同的图表,但默认情况下最初取消选中某些列,并保留选中或取消选中它们以进行可视化的选项。 This means that I cannot take only a subset of columns as new data frame to show as the other columns are still relevant.
这意味着我不能只将列的子集作为新数据框来显示,因为其他列仍然相关。 Did not find anything in the documentation unfortunately...
不幸的是,在文档中没有找到任何东西......
Thank you in advance.先感谢您。
You can use the visible
property of the traces to state it is only in the legend.您可以使用迹线的
visible
属性到 state 它仅在图例中。 Below shows all columns in the figure then first two columns are set as visible, all other columns are only in the legend.下面显示了图中的所有列,然后前两列设置为可见,所有其他列仅在图例中。
import plotly.express as px
import pandas as pd
import numpy as np
# simulate dataframe
df = pd.DataFrame(
{c: np.random.uniform(0, 1, 100) + cn for cn, c in enumerate("ABCDEF")}
)
fig = px.line(df, x=df.index, y=df.columns)
# for example only display first two columns of data frame, all others can be displayed
# by clicking on legend item
fig.for_each_trace(
lambda t: t.update(visible=True if t.name in df.columns[:2] else "legendonly")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.