Manu Sharma
Manu Sharma

Reputation: 1739

Matplotlib multiple subplots figure spacing as per requirement

A very simple question regarding spacing the matplotlib subplots, despite multiple iterations on plt.tight_layout or plt.subplots_adjust. I am still not able to bring the right space between the subplots

Following is a sample python code

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline


dict1 = {'importance': {'var1': 67.18,'var2': 50.33,'var3': 29.33,
   'var4': 27.17, 'var5': 24.880}}

dict2 = {'importance': {'var3': 50.18,'var1': 30.33,'var2': 29.33,
  'var4': 24.17, 'var5': 7.880}}


df1 = pd.DataFrame.from_dict(dict1)
df2 = pd.DataFrame.from_dict(dict2)


fig, (ax1, ax2) = plt.subplots(1,2)
fig = plt.figure(figsize=(60,60))
fig.subplots_adjust(wspace=10)
df1.plot(kind = 'barh', ax = ax1)
df2.plot(kind = 'barh', ax = ax2)

So the graph isenter image description here

which is certainly not I am looking for, I am looking for a wide spread subplots. Pls Help

Upvotes: 1

Views: 198

Answers (2)

Joe
Joe

Reputation: 12417

What you can do is:

fig, (ax1, ax2) = plt.subplots(1,2,figsize=(10,10))
fig.subplots_adjust(wspace=0.5)
df1.plot(kind = 'barh', ax = ax1)
df2.plot(kind = 'barh', ax = ax2)
plt.show()

Output: enter image description here

Upvotes: 1

Patol75
Patol75

Reputation: 4547

Your problem here is that fig = plt.figure(figsize=(60,60)) creates a second figure, so when you call fig.subplots_adjust(wspace=10), it modifies this second figure and not the one on which you have your plotting axes. A way to correct this is to first include the keyword figsize inside your call to subplots fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(60, 60), then remove the call to figure underneath and finally adjust the wspace value.

Upvotes: 2

Related Questions