在绘图中创建一个下拉菜单

时间:2018-09-15 17:42:25

标签: pandas plotly

我有以下数据框:

# Create DataFrame
df = pd.DataFrame({"Col_A_date":[2018-09-04,2018-09-05,2018-09-04,2018-09-05],
                   "Col_B_hour":[7,7,8,8],
                   "Col_C":[1,1,2,2],
                    "Col_value":[1.9,2.2,2.6,3.8]
                   })

我想创建一个图形,其中 col_A显示为下拉菜单(2018-09-04和2018-09-05),Col_B为x轴,Col_value为y轴,Col_C为迹线< / strong>。因此,我可以在同一张图中看到不同日期的数据。可以使用plotly吗?

1 个答案:

答案 0 :(得分:1)

是的,有可能。更新了您的解释。 如果我正确理解您的需求,那么该代码就能满足您的需求:

# import libraries
import pandas as pd
import plotly
import plotly.graph_objs as go

# Create DataFrame
df = pd.DataFrame({"Col_A_date":["2018-09-04","2018-09-05","2018-09-04","2018-09-05"],
                   "Col_B_hour":[7,7,8,8],
                   "Col_C":[1,1,2,2],
                   "Col_value":[1.9,2.2,2.6,3.8]
                   })
# create four df for traces
df1 = df.loc[(df["Col_A_date"] == "2018-09-04") & (df["Col_C"] == 1)]
df2 = df.loc[(df["Col_A_date"] == "2018-09-04") & (df["Col_C"] == 2)]
df3 = df.loc[(df["Col_A_date"] == "2018-09-05") & (df["Col_C"] == 1)]
df4 = df.loc[(df["Col_A_date"] == "2018-09-05") & (df["Col_C"] == 2)]
print(df1,df2,df3,df4)
# Create traces
trace1 = go.Bar(x=list(df1["Col_B_hour"]),
                y=list(df1["Col_value"]),
                name="1",
                text = list(df1["Col_value"]),
                textposition="auto",
                hoverinfo="name",
                marker=dict(color="rgb(0,102,204)")
                )
trace2 = go.Bar(x=list(df2["Col_B_hour"]),
                y=list(df2["Col_value"]),
                name="2",
                text=list(df2["Col_value"]),
                textposition="auto",
                hoverinfo="name",
                marker=dict(color="rgb(255,128,0)")
                )
trace3 = go.Bar(x=list(df3["Col_B_hour"]),
                y=list(df3["Col_value"]),
                name="3",
                text = list(df3["Col_value"]),
                textposition="auto",
                hoverinfo="name",
                marker=dict(color="rgb(255,178,102)")
                )
trace4 = go.Bar(x=list(df4["Col_B_hour"]),
                y=list(df4["Col_value"]),
                name="4",
                text=list(df4["Col_value"]),
                textposition="auto",
                hoverinfo="name",
                marker=dict(color="rgb(255,255,153)")
                )
# Pull traces to data
data = [trace1,trace2,trace3,trace4]
# Specify dropout parameters
updatemenus = list([
    dict(active=-1,
         buttons=list([   
            dict(label = "4 Aug 1",
                 method = "update",
                 args = [{"visible": [True, False, False, False]},
                         {"title": "4 Aug 1"}]),
            dict(label = "4 Aug 2",
                 method = "update",
                 args = [{"visible": [False, True, False, False]},
                         {"title": "4 Aug 2"}]),
            dict(label = "5 Aug 1",
                 method = "update",
                 args = [{"visible": [False, False, True, False]},
                         {"title": "5 Aug 1"}]),
            dict(label = "5 Aug 2",
                 method = "update",
                 args = [{"visible": [False, False, False, True]},
                         {"title": "5 Aug 2"}]),
            dict(label = "All",
                 method = "update",
                 args = [{"visible": [True, True, True, True]},
                         {"title": "All"}]),
            dict(label = "Reset",
                 method = "update",
                 args = [{"visible": [False, False, False, False]},
                         {"title": "Reset"}])
        ]),
    )
])
# Set layout
layout = dict(title="Dropdown",
              showlegend=False,
              xaxis=dict(title="Hours"),
              yaxis=dict(title="Number"),
              updatemenus=updatemenus)
# Create fig
fig = dict(data=data, layout=layout)
# Plot the plotly plot
plotly.offline.plot(fig, filename="update_dropdown.html")

选择All如下所示: All 首先是traceFirst trace 这里有一些来自文档的有用链接:关于bar chartshover text; dropdown menu。不要害怕看plotly文档-有很好的示例说明如何正确使用此软件包。