Note Stackoverflow link of same question: https://stackoverflow.com/questions/55342231/possible-bug-in-plotly-xticklabel-assigning-for-plot-with-muliple-plots
Question:
In the following image why correct category 28 is shown as category_5 in the xticklabel?
Xticklabel ordering seems to be wrong here, how can it be fixed?
I checked the value of category_28 and it is correct, but why the plotly is showing it as the label Category_5
?
import numpy as np
import pandas as pd
import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools
from plotly.offline import iplot, init_notebook_mode, plot
init_notebook_mode(connected=False) # open in new tab, not in jupyter
df = pd.read_csv('https://github.com/bhishanpdl/Datasets/blob/master/Historical%20Product%20Demand.csv?raw=true')
df['Date'] = pd.to_datetime(df['Date'])
df['Order_Demand'] = pd.to_numeric(df.Order_Demand, errors = 'coerce')
df = df.dropna()
df = df.sort_values('Date')
df['Year'] = df.Date.dt.year
df['Month'] = df.Date.dt.month
df.head()
def plot_category_vs_demand_all_years2(warehouse_name):
df1 = pd.DataFrame(df[df['Warehouse'] == warehouse_name])
# yearly data
df1_yearly = []
years = [2012,2013,2014,2015,2016]
for year in years:
df_this_year = (df1[df1['Year'] == year]
.groupby('Product_Category', as_index=False)['Order_Demand']
.mean()
.sort_values('Order_Demand', ascending=False))
df1_yearly.append(df_this_year)
# traces
traces = []
for year, dfy in zip(years,df1_yearly):
traces.append(go.Bar(x=dfy['Product_Category'],
y=dfy['Order_Demand'],
name='Year_' + str(year)))
# create fig
fig = tools.make_subplots(rows=2, cols=len(years))
for i in range(len(years)):
fig.append_trace(traces[i], 1, i+1)
plot(fig, filename='stacked-subplots.html')
plot_category_vs_demand_all_years2('Whse_A')