【问题标题】:How to make 18 separate scatterplots for 18 different columns from the same pandas Dataframe?如何为来自同一个熊猫数据框的 18 个不同列制作 18 个单独的散点图?
【发布时间】:2015-10-13 20:09:40
【问题描述】:

如何根据以下信息制作三个小散点图:

  • 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()

【问题讨论】:

  • 我认为您需要列出大于 5 的值和

标签: python pandas subplot scatter


【解决方案1】:

您可以编写一个快速函数来将值转换为颜色。此外,您需要将数组传递到每个散点图中您只传递一个。

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()

【讨论】:

  • 谢谢!刚试过,但一直收到AttributeError: 'numpy.ndarray' object has no attribute 'scatter'
猜你喜欢
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 2017-01-05
  • 2021-07-25
  • 2020-07-25
相关资源
最近更新 更多