1

I have the following dataframe:

# 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]
                   })

I want to create a graph where col_A is shown as drop drown menus (2018-09-04 and 2018-09-05), Col_B is x-axis, Col_value is y-axis and Col_C as traces. So I can see the data for different dates in the same graph. Is it possible to do using plotly?

1 Answer 1

1

Yep, it is possible. Updated with your explanations. If I am correct understand what you need, that`s code deal what you want:

# 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")

Here how choice All looks like: All And first trace: First trace Here some useful links from docs: about bar charts; hover text; dropdown menu. Do not be afraid to look at the plotly documentation - there are excellent examples of how to use this package correctly.

Sign up to request clarification or add additional context in comments.

4 Comments

No this is not what I wanted. For df1 there would be 2 traces based on Col_C values so that in trace_1 y = df_2[df_2[Col_C]=1][Col_value] and trace_2 y = df_2[df_2[Col_C]=2][Col_value]. Similarly, df2 there would trace 1 (y = df_2[df_2[Col_C]=1][Col_value]) and trace 2 (y = df_2[df_2[Col_C]=2][Col_value]). In your example Col_C is not included in the figure.
This does answer my question but doesn't solve my problem. This is feasible when you have a small df but in my actual data, I have 7 or more Col_C value and dates would be dynamically updated. I was thinking the drop down menus would have the dates and subplots assigned to that button. Whereas, traces would be common to all subplots.
@dsh-m, hmm. Then you need imagine how your desired plot looks like and add image of it here to better understanding what you are looking for. Maybe ask another question with this new information. Write a well-written question. It is a good way to get an answer that will solve your problem
A perfect solution for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.