Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn: how to set bar borders' line width or color?

I am trying to draw a barplot with bars with no borders. By default bars has thin black borders. In the devlopment version (0.6) of Seaborn, I could pass kwargs (linewidth, edgecolor) to pyplot.bar() via seaborn.barplot(), but in the current version (0.5.1) this feature seems not yet available. Looking at the returned AxesSubplot object, I could not find the way to set the line width to zero, or the color to fully transparent, although it has many methods, so I still hope there is a way to achieve this.

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots()
ax = sns.barplot(data = data, x = 'var1', color = '#007b7f')
fig.tight_layout()
fig.savefig('fig.pdf')
like image 942
deeenes Avatar asked May 25 '15 19:05

deeenes


People also ask

Which is used to set border color of each bar?

The border-color shorthand CSS property sets the color of an element's border.

Which attribute of plot () function is used to set the edge color of bar in bar chart?

The color attribute is used to set the color of the bars(maroon in this case). plt. xlabel(“Courses offered”) and plt. ylabel(“students enrolled”) are used to label the corresponding axes.

How do you set Figsize in Seaborn?

It has a parameter called figsize which takes a tuple as an argument that contains the height and the width of the plot. It returns the figure and the array of axes. While calling the seaborn plot we will set the ax parameter equal to the array of axes that was returned by matplotlib. pyplot.


2 Answers

This will work too:

fig, ax = plt.subplots()
ax = sns.barplot(data = data, x = 'var1', color = '#007b7f')
plt.setp(ax.patches, linewidth=0)
like image 60
mwaskom Avatar answered Oct 03 '22 21:10

mwaskom


After many attempts without success, I asked the question here, and soon I found the solution myself. So here it is: before plotting, the patch.linewidth parameter can be set with seaborn.set_context():

import seaborn as sns

sns.set_context(rc = {'patch.linewidth': 0.0})
ax = sns.barplot(...)
like image 35
deeenes Avatar answered Oct 03 '22 19:10

deeenes