Rafael Rodrigues Santos Rafael Rodrigues Santos - 15 days ago 4
Python Question

Issue with the plot using layout - python

I have 2 sub dataset called

headlamp_water
and
headlamp_crack
:
For each of these 2 sub dataset, I want to plot 2 graph (1 hbar and other boxplot), whicj will be 4 graphs in the end.

I'm using the following code:

def print_top_dealer(data, top, typegraph):
if typegraph == "hbar":
ax = data.Dealer.value_counts().iloc[:top].plot(kind="barh")
ax.invert_yaxis()
else:
ax = plt.boxplot(data['Use Period'], vert=False)

plt.close('all')
ax1 = print_top_dealer(headlamp_water, 15, "hbar")
ax2 = print_top_dealer(headlamp_water, 15, "boxplot")
ax3 = print_top_dealer(headlamp_crack, 15, "hbar")
ax4 = print_top_dealer(headlamp_crack, 15, "boxplot")
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
plt.tight_layout()


enter image description here

And, I'm getting all datas plot into same graph (last one)
How do I plot right these 4 graphs into (2x2) layout?

Thanks in advance

Answer

You create axes when you call plt.subplots, you need to use them.

This should work (I don't have your data to confirm):

def print_top_dealer(data, top, ax, typegraph):
    if typegraph == "hbar":
        data.Dealer.value_counts().iloc[:top].plot(kind="barh", ax=ax)
        ax.invert_yaxis()
    else:    
        ax.boxplot(data['Use Period'], vert=False)

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)

print_top_dealer(data=headlamp_water, top=15, ax=ax1, typegraph="hbar")
print_top_dealer(data=headlamp_water, top=15, ax=ax2, typegraph="boxplot")
print_top_dealer(data=headlamp_crack, top=15, ax=ax3, typegraph="hbar")
print_top_dealer(data=headlamp_crack, top=15, ax=ax4, typegraph="boxplot")

plt.tight_layout()

Since you didn't provide data, here's some dummy one:

headlamp_water = pd.DataFrame(np.random.choice(['A1','A2','A3'], size=10), columns=['Dealer'])

headlamp_crack = pd.DataFrame(np.random.choice(['B1','B2','B3'], size=10), columns=['Dealer'])

headlamp_water['Use Period'] = np.random.rand(10)
headlamp_crack['Use Period'] = np.random.rand(10)

Here's what they look like:

print(headlamp_water)

  Dealer  Use Period
0     A3    0.058678
1     A3    0.734517
2     A1    0.371943
3     A2    0.290254
4     A3    0.869392
5     A3    0.082629
6     A3    0.069261
7     A1    0.089310
8     A3    0.633946
9     A2    0.176956

Now let's try the graph:

def print_top_dealer(data, top, ax, typegraph):
    if typegraph == "hbar":
        data.Dealer.value_counts().iloc[:top].plot(kind="barh", ax=ax)
        ax.invert_yaxis()
    else:    
        ax.boxplot(data['Use Period'], vert=False,)

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)

print_top_dealer(data=headlamp_water, top=15, ax=ax1, typegraph="hbar")
print_top_dealer(data=headlamp_water, top=15, ax=ax2, typegraph="boxplot")
print_top_dealer(data=headlamp_crack, top=15, ax=ax3, typegraph="hbar")
print_top_dealer(data=headlamp_crack, top=15, ax=ax4, typegraph="boxplot")

plt.tight_layout()

SUbplots