簡體   English   中英

如何從同一只熊貓數據框為18個不同的列制作18個單獨的散點圖?

[英]How to make 18 separate scatterplots for 18 different columns from the same pandas Dataframe?

給定以下信息,我如何制作三個小型散點圖:

  • 總共50列的熊貓數據框(df)

df.columns = ["A", "B", "C", "D", "E", (...)]

  • 我要進行三個單獨的散點圖繪制的列表包含18個列名稱:

selection = ["A", "B", "C", (...)]

  • value > 5.0應被塗成blue ,值<= 5.0等於<= 5.0應被塗成red

我嘗試了以下代碼,但是沒有任何提示?

fig, ax = plt.subplots(4, 5)
for column in selection:
    if column in df.columns:
    ax.scatter(df[column], if df[column][value > 5.0]: color = 'r', if df[column][value <= 5.0]: color = 'b')
plt.show()

您可以編寫一個快速功能,將值轉換為顏色。 另外,您需要將數組傳遞到每個散點圖中,只傳遞一個。

import numpy as np

@np.vectorize
def colorUp(x):
    return 'b' if x <=5.0 else 'r'

# Each scatterplot requires two arrays. You are only passing one;
# I am assuming that this would be the second array passed into
# each '.scatter' call
second_array = np.arange(df.shape[0])

fig, ax = plt.subplots(4, 5)
for i, column in enumerate(selection):
    if column in df.columns:
        ax[i % 4, i / 4].scatter(df[column], second_array, c = colorUp(df[column]))
plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM