[英]seaborn not coloring bars in barplot
我已經開始研究我的論文,但我已經遇到了問題。 我正在嘗試做一個 plot 的酒吧。 250 個條,但似乎 seaborn 沒有顯示 colors 對於大多數條。 我認為它可以在調色板中循環 - 但它只顯示它們是白色的。 如果我取一個較小的樣本(我認為最多 99 個),每個條都是彩色的。 即使只有一種顏色(不是調色板),也會顯示白色條。
這是代碼和圖表:
有人可以幫我弄這個嗎? 謝謝!
這是一段代碼來測試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
只是為了讓您知道它適用於這些版本
# 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()
所以它似乎適用於我的代碼數組。
也許:
seaborn 無法理解您的數據(檢查您的數據框?)
您有一個有問題的 seaborn 或 matplotlib 版本(嘗試 pip install seaborn==0.10.0 和 pip install matplotlib==3.1.3)
您可以嘗試其他調色板
希望這可以幫助!
編輯:感謝@ImportanceOfBeingErnest,我想我理解你的問題。 它來自這種風格,只需注釋/取消注釋即可查看結果
# sb.set_style("darkgrid")
遺憾的是,我沒有找到任何與darkgrid 樣式相關的kwargs 選項來刪除該錯誤...所以我建議我們手動重新創建此樣式!
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()
我認為出現這個問題是因為,默認情況下,在seaborn
,條形圖中的barplot
用細白色邊界線勾勒出輪廓。 當條形本身非常細時,您只能看到這個白色邊界。 嘗試將繪圖命令更改為:
g = sb.barplot(x = x, y = y, palette = sb.color_palette("RdBu", n_colors=7), lw=0.)
lw=0.
刪除邊界線,然后您應該會看到條形顏色。
在繪圖之前嘗試sns.reset_orig()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.