創建 matplotlib 顏色條時,可以設定drawedges用True黑線分隔顏色條的顏色。但是,當使用 擴展extend='both'顏色條時,末端的黑線不會出現。那是一個錯誤嗎?是否有可能以其他方式繪制這些邊緣?
這是代碼:
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import from_levels_and_colors
my_cmap = mpl.cm.viridis
bounds = np.arange(10)
nb_colors = len(bounds) 1
colors = my_cmap(np.linspace(100, 255, nb_colors).astype(int))
my_cmap, my_norm = from_levels_and_colors(bounds, colors, extend='both')
plt.figure(figsize=(5, 1))
ax = plt.subplot(111)
cbar = mpl.colorbar.ColorbarBase(ax, cmap=my_cmap, norm=my_norm, orientation='horizontal', drawedges=True)
plt.subplots_adjust(left=0.05, bottom=0.4, right=0.95, top=0.9)
plt.show()
以及它給出的數字:

uj5u.com熱心網友回復:
我從您的問題中對其進行了調查,并找到了一種更改顏色條邊框和垂直線顏色的方法。我用它把它們改成紅色。我得到的結果是擴展的輪廓是紅色的,所以我的猜測是我只是將正常顏色條矩形的短邊向左和向右拉。我發現
所以我認為唯一的方法是添加垂直線。
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import from_levels_and_colors
my_cmap = mpl.cm.viridis
bounds = np.arange(10)
nb_colors = len(bounds) 1
colors = my_cmap(np.linspace(100, 255, nb_colors).astype(int))
my_cmap, my_norm = from_levels_and_colors(bounds, colors, extend='both')
plt.figure(figsize=(6, 2))
ax = plt.subplot(111)
cbar = mpl.colorbar.ColorbarBase(ax, cmap=my_cmap, norm=my_norm, orientation='horizontal', drawedges=True)
# update
cbar.outline.set_edgecolor('red')
cbar.dividers.set_color('red')
plt.axvline(max(bounds), color='red', alpha=0.3, linewidth=3.5)
plt.axvline(min(bounds), color='red', alpha=0.3, linewidth=3.5)
plt.subplots_adjust(left=0.05, bottom=0.4, right=0.95, top=0.9)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461696.html
標籤:Python matplotlib 彩条 颜色图
