我計算了資料框的 NaN 值百分比,然后將其繪制出來。我希望每個變數都有唯一的顏色。我使用的代碼效果很好,但每 9 個可變顏色與第一個可變顏色相同,并且回圈重復。見圖片:
編碼:
per = df.isna().mean().round(4) * 100
f, ax = plt.subplots(figsize=(25, 12), dpi = 200)
i = 0
for key, value in zip(per.keys(), per.values):
if (value > 0):
ax.bar(key, value, label=key)
ax.text(i, value 0.5, str(np.round(value, 2)), ha='center')
i = i 1
ax.set_xticklabels([])
ax.set_xticks([])
plt.title('NaN Value percentage in the dataset')
plt.ylim(0,115)
plt.ylabel('Percentage')
plt.xlabel('Columns')
plt.legend(loc='upper left')
plt.show()
我嘗試了以下代碼行,但它只選擇了第一種顏色:
my_colors = list(islice(cycle(['b', 'r', 'g', 'y', 'c', 'm',
'tan', 'grey', 'pink', 'chocolate', 'gold']), None, len(df)))
f, ax = plt.subplots(figsize=(25, 12), dpi = 200)
i = 0
for key, value in zip(per.keys(), per.values):
if (value > 0):
ax.bar(key, value, label=key, color = my_colors)
ax.text(i, value 0.5, str(np.round(value, 2)), ha='center')
i = i 1
ax.set_xticklabels([])
ax.set_xticks([])
plt.title('NaN Value percentage in the dataset')
plt.ylim(0,115)
plt.ylabel('Percentage')
plt.xlabel('Columns')
plt.legend(loc='upper left')
plt.show()
結果:
任何幫助表示贊賞。
或非隨機(注釋掉random.shuffle(lst)
):
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/486594.html
標籤:Python 熊猫 matplotlib 条形图 楠