設定顏色 alpha 值的方式#

比較透過 *alpha* 關鍵字引數和其中一種 Matplotlib 顏色格式設定 alpha。通常,*alpha* 關鍵字是為顏色新增透明度的唯一工具。在某些情況下,*(matplotlib_color, alpha)* 顏色格式提供了一種微調圖形外觀的簡單方法。

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility.
np.random.seed(19680801)

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4))

x_values = [n for n in range(20)]
y_values = np.random.randn(20)

facecolors = ['green' if y > 0 else 'red' for y in y_values]
edgecolors = facecolors

ax1.bar(x_values, y_values, color=facecolors, edgecolor=edgecolors, alpha=0.5)
ax1.set_title("Explicit 'alpha' keyword value\nshared by all bars and edges")


# Normalize y values to get distinct face alpha values.
abs_y = [abs(y) for y in y_values]
face_alphas = [n / max(abs_y) for n in abs_y]
edge_alphas = [1 - alpha for alpha in face_alphas]

colors_with_alphas = list(zip(facecolors, face_alphas))
edgecolors_with_alphas = list(zip(edgecolors, edge_alphas))

ax2.bar(x_values, y_values, color=colors_with_alphas,
        edgecolor=edgecolors_with_alphas)
ax2.set_title('Normalized alphas for\neach bar and each edge')

plt.show()
Explicit 'alpha' keyword value shared by all bars and edges, Normalized alphas for each bar and each edge

參考

此範例中顯示了下列函數、方法、類別和模組的使用

標籤:樣式:顏色 繪圖類型:長條圖 級別:初學者

由 Sphinx-Gallery 產生的圖庫