![](/img/trans.png)
[英]Plot histogram using Python with different colors for positive and negative values
[英]Drawing plot by positions with different colors using python
使用 seaborn 怎么樣:
import seaborn as sns
colors = df.groupby(['1','2']).ngroup().astype('category')
sns.barplot(x=df['position'], y=1, hue=colors, dodge=False)
Output:
或者您可以手動 plot 條形圖,這樣可以正確縮放position
:
cmap = {
('x','o'): 'b',
('o','x'): 'r',
('o','o'): 'g',
('x','x'): 'm'
}
fig, ax = plt.subplots()
for _, row in df.iterrows():
ax.bar(row['position'], 1, edgecolor=cmap[(row['1'], row[2])],
facecolor=(0,0,0,0),
width=10)
Output:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.