2016-08-08 78 views

回答

1

我使用seaborn barplotpalette選項。

df = pd.DataFrame({'a':[1,2,3,4,5], 'b':[10,5,2,4,5]}) 

使用seaborn:

sns.barplot(df['a'], df['b'], palette='Blues_d') 

可以獲得類似:像想象一下,你有一個簡單的數據幀

enter image description here

那麼你也可以用palette選項玩, colormap根據像一些數據相加的梯度:

sns.barplot(df['a'], df['b'], palette=cm.Blues(df['b']*10) 

獲得:

enter image description here

希望有所幫助。

+0

非常感謝,但是我正在尋找相同的梯度每一個酒吧內,沒有一個梯度翻過不同吧! – cattt84

0

正如在Pyplot: vertical gradient fill under curve?描繪的一個可使用圖像來創建梯度曲線。

由於杆是矩形的圖像的程度,可直接設置到欄的位置和大小。人們可以遍歷所有酒吧並在相應的位置創建圖像。結果是一個梯度條形圖。

import numpy as np 
import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 

bar = ax.bar([1,2,3,4,5,6],[4,5,6,3,7,5]) 

def gradientbars(bars): 
    grad = np.atleast_2d(np.linspace(0,1,256)).T 
    ax = bars[0].axes 
    lim = ax.get_xlim()+ax.get_ylim() 
    for bar in bars: 
     bar.set_zorder(1) 
     bar.set_facecolor("none") 
     x,y = bar.get_xy() 
     w, h = bar.get_width(), bar.get_height() 
     ax.imshow(grad, extent=[x,x+w,y,y+h], aspect="auto", zorder=0) 
    ax.axis(lim) 

gradientbars(bar) 

plt.show() 

enter image description here