John Conor
John Conor

Reputation: 884

Python Plotly: Not maintaining axes when using String formatted dates instead of Numeric

I'm encountering a strange problem with plotly and pandas where the axes are flipping when I use a formatted date string instead of an int for the month.

I created this group from my dataset:

dfg = dfq.groupby(['Target', 'Mon','Month_fr','Year'])['Value'].mean().reset_index()

Target    Mon Mon_fr Yr     Value
Aggression  1   Jan 2020    0.393939
Aggression  2   Feb 2020    0.181818
Aggression  3   Mar 2020    0.430556
Aggression  4   Apr 2020    0.159420
Aggression  5   May 2020    0.445783
Aggression  6   Jun 2020    0.446154
Aggression  7   Jul 2019    0.620690
Aggression  8   Aug 2019    0.692308
Aggression  9   Sep 2019    0.207792
Aggression  10  Oct 2019    0.287671
Aggression  11  Nov 2019    0.253968
Aggression  12  Dec 2019    0.461538
Off-Task    1   Jan 2020    0.818182
Off-Task    2   Feb 2020    0.606061
Off-Task    3   Mar 2020    0.643836
Off-Task    4   Apr 2020    0.710145
Off-Task    5   May 2020    1.084337
Off-Task    6   Jun 2020    1.014925
Off-Task    7   Jul 2019    1.137931
Off-Task    8   Aug 2019    0.846154
Off-Task    9   Sep 2019    0.701299
Off-Task    10  Oct 2019    0.794521
Off-Task    11  Nov 2019    0.730159
Off-Task    12  Dec 2019    0.984615
SIB         1   Jan 2020    0.492308
SIB         2   Feb 2020    0.045455
SIB         3   Mar 2020    0.154930
SIB         4   Apr 2020    0.289855
SIB         5   May 2020    0.192771
SIB         6   Jun 2020    0.164179
SIB         7   Jul 2019    0.086207
SIB         8   Aug 2019    0.415385
SIB         9   Sep 2019    0.434211
SIB         10  Oct 2019    0.301370
SIB         11  Nov 2019    0.245902
SIB         12  Dec 2019    0.507692

and wish to plot side by side bar charts with "Value" on the y-axis

This works fine when I use the numeric month value:

fig = px.bar(dfg, x=["Target", "Mon"], y="Value", color = "Target",title="Behavior Data",barmode="group")
fig.show() 

enter image description here

But when I try to do the same using formatted month variable:

fig = px.bar(dfg, x=["Target", "Month_fr"], y="Value", color = "Target",title="Behavior Data",barmode="group")
fig.show() 

It flips the axes and no longer uses "Values" as the data being represented:

enter image description here

Have any of you good people came across this bizarre problem before?! HELP...

Upvotes: 0

Views: 61

Answers (1)

John Conor
John Conor

Reputation: 884

I got it! The issue was that I had the values for x entered as a list:

x=["Target", "Month_fr"],

Unbeknownst to me I didn't need to include "Target" in my x variable as that grouping is handled by "color = 'Target'"... quite why this wan't a problem when using an integer for the months I don't know. There may be some issue with using 2 string values as the x value.

Thanks for anyone who considered! Here is the working code if you happen to be interested:

import plotly.express as px  # (version 4.7.0)

fig = px.bar(dfg, x="Month_fr", y="Value", color = "Target",title="Behavior Data",barmode="group")
fig.show() 

Upvotes: 1

Related Questions