'Adding a line to a barplot in Seaborn pads the graph, how do I get rid of the new margins?
I have a seaborn barplot with a line graphed on top of it that looks like this:
As you can see, there are thick margins on the plot that appear after adding the line. The plot looks normal if the line is not added:
plt.margins() does not work and basically destroys the entire graph. Not sure why. I have not tried additional rc-settings in sns.set() because I cannot find the page that lists all the options anymore, so if someone could link that as well so I could bookmark it that would be appreciated.
The barplot is created with:
sns.set(rc={'figure.figsize': (50, 10), 'axes.labelsize': 25, 'ytick.labelsize': 17, 'axes.labelpad': 15, 'legend.fontsize': 20})
fig, ax1 = plt.subplots()
g = sns.barplot(x='City/Town', y="Value", hue="Metric", data=df, ax=ax1)
g.set_title(title, fontsize=30)
fig.autofmt_xdate()
and the line is added with:
ax1.plot(range(len(X)), df.Y_pred[df['Metric'] == bar1]*scale, color='red')
where X and Y are numpy ndArrays and scale is just an int.
Solution 1:[1]
I use subplots with a lineplot on top and marplot below. lineplot always has more padding so need to sync them. rcParams does not work for both so have to individually set on the axes:
for ax in [ax1, ax2]:
ax.margins(x=0.02)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jmoz |


