每當我制作子圖時,我習慣于單獨制作每個圖并將它們全部組合在一個串列中。
Pandas 真的很酷,因為只需一行簡單的代碼,我就可以制作一個包含資料集所有特征的直方圖的圖形:
hist_plot = iris.hist(alpha=0.4, figsize=(10,6))

但是,我對如何更改這個數字感到困惑。例如:
- 使每個圖具有不同的顏色:
color = ['blue', 'red', 'green', 'purple'] - 給每個情節一個不同的標題:
title = ['SLength', 'SWidth', 'PLength', 'PWidth'] - 為每個 y 軸添加“頻率”標簽
有沒有辦法做到這一點而不必單獨制作每個情節?我以這個為例進行了嘗試,但它沒有用:
hist_plot = iris.hist(color = ['blue', 'red', 'green', 'purple'], alpha=0.4, figsize=(10,6))
uj5u.com熱心網友回復:
df.hist()回傳坐標區物件的二維陣列,它們對應于網格排列中的子圖。為了迭代它們,它們的flat屬性很方便,因為它在一個平面陣列中包含相同的物件。您可以使用zip()并行迭代子圖以及顏色和標題串列:
from sklearn.datasets import load_iris
iris = load_iris(as_frame=True)['data']
colors = ['blue', 'red', 'green', 'purple']
titles = ['SLength', 'SWidth', 'PLength', 'PWidth']
axs = iris.hist(alpha=0.4, figsize=(10, 6))
for ax, color, title in zip(axs.flat, colors, titles):
for patch in ax.patches:
patch.set_color(color)
ax.set_title(title)
ax.set_ylabel('Frequency')

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/480630.html
標籤:Python 熊猫 matplotlib 阴谋
上一篇:Python從串列中創建3d圖
