Home > Mobile >  Seaborn/Matplotlib: how to remove the horizontal white lines that are overlaying the bars?
Seaborn/Matplotlib: how to remove the horizontal white lines that are overlaying the bars?

Time:01-23

This is how it looks:

enter image description here

I would like to remove the white lines that are overlaying the black bars. Btw: is it possible to remove the background behind the legend?

`def stack(): data1 = [

    0.7,
    0.8,
    0.3,
    0.6,
    0.5

]

data2 = [

    20, 30, 23, 17, 28

]
sns.set_theme()
data = np.multiply(data1, 100)

r = [0, 1, 2, 3, 4]

fig, ax1 = plt.subplots()
ax1.bar(r, data, color="black", width=.5)
plt.ylim(0,100)
plt.ylabel('Percent')
plt.xlabel('Lineage')
ax2 = ax1.twinx()
ax2.bar(r, data2, color="red", width=.1)
plt.ylim(0,150)
plt.ylabel("Number")

lgnd1 = mpatches.Patch(color="black", label='Percent')
lgnd2 = mpatches.Patch(color="red", label='Number')
plt.legend(loc='upper center',
           bbox_to_anchor=(0.5, 1.2),
           ncol=3, handles=[lgnd1, lgnd2])

plt.savefig('number.svg', bbox_inches="tight", transparent=True)
plt.show()`

CodePudding user response:

Regarding the color of your legend: You chose this grey by setting the plot default to be the seaborn standard with sns.set_theme() (see seaborn.set_theme).

But, as described there, you are able to override every rc parameter ("runtime configuration parameter") Setting the legend background color to another color should be possible with using the parameter facecolor=... (see matplotlib.pyplot.legend, scroll down a bit )

In your case you can add this parameter here in your legend definition:

plt.legend(loc='upper center',
           facecolor='white',  # choose your background color
           bbox_to_anchor=(0.5, 1.2),
           ncol=3, handles=[lgnd1, lgnd2])

CodePudding user response:

You can use the following code :

plt.grid(False)

Or if you still want the lines you can use:

plt.grid(zorder=0)
plt.bar(range(len(y)), y, width=0.3, align='center', color='skyblue', 
zorder=3)
  • Related