简体   繁体   中英

seaborn not coloring bars in barplot

I have started working on my thesis and I already have a problem. I am trying to do a bar plot of ca. 250 bars, but it seems like that seaborn is not showing colors for most of the bars. I thought it could cycle through the palette - but it just shows them white. If I take a smaller sample (up to 99 I think), every bar is colored. Even having one single color (not a palette), shows white bars.

Here is the code and the graph:

Can someone help me with this? Thank you!

Here's a piece of code to test the palettes of seaborn:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sb

print("Seaborn version : {0}".format(sb.__version__))
# Seaborn version : 0.10.0

print("Matplotlib version : {0}".format(matplotlib.__version__))
# Matplotlib version : 3.1.3

Just to let you know it worked with these versions

# simple color test without palette
n = 1000
x = np.arange(1,n+1,1)
y = np.random.randint(-100,100,n)*np.random.random(n)
sb.barplot(x,y)
plt.xticks([])
plt.show()

在此处输入图片说明

# with your color choices
plt.figure(figsize=(20,20))
sb.barplot(x,y, palette=sb.color_palette("RdBu",n_colors=7))
plt.xticks([])
plt.show()

在此处输入图片说明

So it seems to work with the arrays of my code.

Maybe:

  • Your data is not understood by seaborn (check you dataframe?)

  • You have a bugged version of seaborn or matplotlib (try pip install seaborn==0.10.0 and pip install matplotlib==3.1.3)

  • You can try another palette of color

Hope this helps!

EDIT: Thanks to @ImportanceOfBeingErnest, I think I understood your problem. It cames from this style, just comment/uncomment to see the result

# sb.set_style("darkgrid")

Sadly, I did not find any kwargs option related to the darkgrid style to remove the bug... So I propose we recreate this style manually!

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('whitesmoke')
# background color
ax.grid(color='white', linestyle='-', linewidth=1)
# the grid
sb.barplot(x,y,zorder=2, palette=sb.color_palette("RdBu",n_colors=7))
#zorder=2 to draw above the grid
plt.xticks([])
plt.show()

在此处输入图片说明

I think that this problem arises because, by default in seaborn , the bars in a barplot are outlined with a thin white boundary line. When the bars themselves are very thin this white boundary is the only thing that you see. Try changing the plot command to:

g = sb.barplot(x = x, y = y, palette = sb.color_palette("RdBu", n_colors=7), lw=0.)

The lw=0. removes the boundary line and you should then see the bar colours.

Try sns.reset_orig() before plotting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM