如何从同一个 pandas Dataframe 中为 18 个不同的列制作 18 个独立的散点图?
How to make 18 separate scatterplots for 18 different columns from the same pandas Dataframe?
如何根据以下信息制作三个小散点图:
- 一个 pandas 数据框
(df)
总共有 50 列:
df.columns = ["A", "B", "C", "D", "E", (...)]
- 一个包含 18 个列名的列表,我想为其制作三个单独的散点图:
selection = ["A", "B", "C", (...)]
- 具有
value > 5.0
的点应着色为 blue
,具有值 <= 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()
如何根据以下信息制作三个小散点图:
- 一个 pandas 数据框
(df)
总共有 50 列:
df.columns = ["A", "B", "C", "D", "E", (...)]
- 一个包含 18 个列名的列表,我想为其制作三个单独的散点图:
selection = ["A", "B", "C", (...)]
- 具有
value > 5.0
的点应着色为blue
,具有值<= 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()