我從熊貓資料幀計算了這個直方圖。

我想根據 x 軸值更改顏色。
例如:
If the value is = 0 the color should be green
If the value is > 0 the color should be red
If the value is < 0 the color should be yellow
我只關心 x 軸。酒吧的高度對我來說并不重要。所有其他解決方案均針對 y 軸。
uj5u.com熱心網友回復:
對于 中的每個條塊ax.containers[0],
uj5u.com熱心網友回復:
只需將它們一一繪制:
import matplotlib as mpl
import matplotlib.pyplot as plt
x = np.linspace(-1,1,10)
y = np.random.uniform(0,1,10)
width = 0.2
plt.figure(figsize = (12, 6))
cmap = mpl.cm.RdYlGn.reversed()
norm = mpl.colors.Normalize(vmin=0, vmax=10)
for x0, y0 in zip(x,y):
plt.bar(x0, y0, width = width, color = cmap(norm(np.abs(x0*10))))

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374589.html
標籤:Python 熊猫 matplotlib 直方图
