1

Say we have two datasets below:

data1 = {
         'A' : pd.Series([10, 20, 30, 40], index = [1, 2, 3, 4]), 
         'B' : pd.Series([8, 15, 29, 42], index = [1, 2, 3, 4]),
         'C' : pd.Series([7, 13, 31, 45], index = [1, 2, 3, 4])
        }

data2 = {
         'A' : pd.Series([9, 18, 27, 38], index = [1, 2, 3, 4]), 
         'B' : pd.Series([11, 17, 28, 41], index = [1, 2, 3, 4]),
         'C' : pd.Series([12, 17, 29, 42], index = [1, 2, 3, 4])
        }

I can represent the data1 using a grouped bar chart, where the x-axis is have ticks [1, 2, 3, 4]. Within each tick, I can have three sections 'A', 'B' and 'C' and I can form barchart with the values in the dataset. A generic example of a grouped bar chart is here.

I can also form a similar group bar chart for data2.

Is there a way that I can combine these two group charts in a single plot? Is there any other kind of chart that can be used to represent both data1 and data2 in a single plot?

Thanks

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
Zac
  • 21
  • 1

1 Answers1

0

You can do a back to back barchart.. or whatever it's called, with some modification from the solution here :

import matplotlib.pyplot as plt
import pandas as pd

fig, axes = plt.subplots(ncols=2, sharey=True)
pd.DataFrame(data1).plot(kind="barh",ax=axes[0])
axes[1]= pd.DataFrame(data2).plot(kind="barh",ax=axes[1])
axes[0].invert_xaxis()
plt.show()

enter image description here

The other option is to use a facet:

import seaborn as sns
data1 = pd.DataFrame(data1).reset_index()
data2 = pd.DataFrame(data2).reset_index()
df = pd.concat([data1,data2])
df['dataset'] = np.repeat(['1','2'],[4,4])

sns.catplot(data=df.melt(id_vars=["index","dataset"]),aspect=1,
            x="dataset",y="value",col="index",hue="variable",kind="bar")

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • That might work if `data1` and `data2` has a significant difference. For eg. in your graph, the top group looks exactly the same until I look very closely. Basically, it is difficult to see the comparison. – Zac Oct 12 '20 at 18:58
  • Ok so can you be clear about what you want to see or what you have tried to code. Seems like there is no clear answer to the question – StupidWolf Oct 12 '20 at 19:00
  • Alright. So, I tried plotting a group bar chart for `data1` and then plot `data2` on top of it as a scatter plot. I use colour-coding and marker-styles to separate `A`, `B` and `C`'s dots. I thought it was an okay solution but I thought maybe there is a better way to do it. – Zac Oct 12 '20 at 19:04
  • You can try with a facet.. – StupidWolf Oct 12 '20 at 19:16
  • That is also an interesting solution. – Zac Oct 12 '20 at 19:39