I am trying to plot an unstacked bar chart for pairs of products differentiated by the year. I.e. what is the difference in X for product 1 between 2020 and 2021?
I have a challenging dataframe structure and thus not sure how I can shape it to fit Plotly's unstacked bar chart framework? I hope someone can guide.Thanks
dataframe:
Date | Shoes__sale_x | Category_x | Shoes__sale_y | Category_y
Jan | $20 | 2020 | $25 | 2021
Feb | $24 | 2020 | $75 | 2021
I want the bar chart to look like this where the legend would display the Category (i.e. 2020 or 2021). How would I best do this?
CodePudding user response:
- structure your data frame with pandas
wide_to_long()
- use plotly express to generate from structured data
import io
import pandas as pd
import plotly.express as px
df = pd.read_csv(io.StringIO("""Date | Shoes__sale_x | Category_x | Shoes__sale_y | Category_y
Jan | $20 | 2020 | $25 | 2021
Feb | $24 | 2020 | $75 | 2021"""), sep="\s \|\s ", engine="python")
df2 = pd.wide_to_long(df, stubnames=["Shoes__sale_", "Category_"], i="Date", j="Sale", suffix=r'\w ').reset_index()
df2["Shoes__sale_"] = df2["Shoes__sale_"].str.strip("$").astype(float)
px.bar(df2, x="Date", y="Shoes__sale_", color="Sale", hover_data=["Category_"], barmode="group")
CodePudding user response:
You can try:
import plotly.express as px
df_sub = df.melt(id_vars=['Date'], value_vars=['Shoes__sale_x', 'Shoes__sale_y'], var_name='sales')
df_sub['value'] = df_sub['value'].str[1:].astype(int)
fig = px.bar(df_sub, x="Date", y="value", color='sales', barmode='group', height=600)
fig.update_layout(yaxis_title='Price', title='Chart Title')
fig.show()
Plot