Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a barplot and a lineplot in the same seaborn plot with different Y axes nicely?

I have two different sets of data with a common index, and I want to represent the first one as a barplot and the second one as a lineplot in the same graph. My current approach is similar to the following.

ax = pt.a.plot(alpha = .75, kind = 'bar')
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(), pt.b.values, alpha = .75, color = 'r')

And the result is similar to this

A barplot and lineplot of the same data

This image is really nice and almost right. My only problem is that ax.twinx() seems to create a new canvas on top of the previous one, and the white lines are clearly seen on top of the barplot.

Is there any way to plot this without including the white lines?

like image 963
Martín Fixman Avatar asked Jun 20 '16 19:06

Martín Fixman


People also ask

How do you plot multiple lines in Seaborn?

You probably need to re-organize your dataframe in a suitable way so that there is one column for the x data, one for the y data, and one which holds the label for the data point. You can also just use matplotlib. pyplot . If you import seaborn , much of the improved design is also used for "regular" matplotlib plots.

How do you set Xlabel and Ylabel in Seaborn?

Use the set_xlabel() and set_ylabel() Functions to Set the Axis Labels in a Seaborn Plot. A seaborn plot returns a matplotlib axes instance type object. We can use the set_xlabel() and set_ylabel to set the x and y-axis label respectively. We can use the fontsize parameter to control the size of the font.


1 Answers

You can use twinx() method along with seaborn to create a seperate y-axis, one for the lineplot and the other for the barplot. To control the style of the plot (default style of seaborn is darkgrid), you can use set_style method and specify the preferred theme. If you set style=None it resets to white background without the gridlines. You can also try whitegrid. If you want to further customize the gridlines, you can do it on the axis level using the ax2.grid(False).

import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

matplotlib.rc_file_defaults()
ax1 = sns.set_style(style=None, rc=None )

fig, ax1 = plt.subplots(figsize=(12,6))

sns.lineplot(data = df['y_var_1'], marker='o', sort = False, ax=ax1)
ax2 = ax1.twinx()

sns.barplot(data = df, x='x_var', y='y_var_2', alpha=0.5, ax=ax2)

A barplot and lineplot seaborn example sharing the same x-axis and different y-axis

like image 89
athina.bikaki Avatar answered Oct 01 '22 02:10

athina.bikaki