散点图(matplotlib)(python)出现关键错误。

问题描述 投票:0回答:1

我有一个数据框架

    artist  bpm nrgy    dnce    dB  live    spch    val acous
20  drake   112.0   26.0    49.0    -17.0   7.0 9.0 31.0    65.0
35  drake   100.0   41.0    77.0    -7.0    7.0 10.0    29.0    0.0
36  drake   152.0   57.0    64.0    -7.0    9.0 11.0    43.0    37.0
37  drake   122.0   52.0    63.0    -10.0   9.0 27.0    30.0    3.0
47  drake   172.0   57.0    75.0    -8.0    53.0    48.0    55.0    38.0
48  drake   100.0   24.0    70.0    -9.0    11.0    5.0 38.0    62.0

我想创建一个散点图,但我一直遇到键的问题。请帮助我,谢谢

    fig = plt.figure(figsize=(12, 18))

current = 1
for col in columns:
    plt.subplot(5, 2, current) # 5 rows, 2 histograms per row
    current += 1 # looping over to the next measure
    plt.plot(df_artist.index, df_artist[col], data=df_artist, linestyle='none', marker='o') 
    plt.title(col)

plt.show()

我一直得到一个关键错误和一个空的情节:(

谢谢你!我有一个数据框艺术家bpm nrgy dn的数据。

python pandas dataframe matplotlib scatter-plot
1个回答
0
投票

假设你想用情节(而不是直方图)来绘制散点图。

你的代码必须是这样的

fig = plt.figure(figsize=(12, 18))

current = 1
for col in df.columns:
    plt.subplot(5, 2, current) # 5 rows, 2 histograms per row
    current += 1 # looping over to the next measure
    df_x = np.array(df.index)
    df_y = np.array(df[col])
    plt.plot(df_x, df_y, linestyle='none', marker='o') 
    plt.title(col)

plt.show()

为什么你的代码不能用?

您之所以会出现按键错误,是因为您的 df.index 是一个可突变的列表,而可突变的对象不能被哈希。因此,你得到了错误。

为什么新的代码可以工作?

我只是简单地将值列表转换为numpy数组,其中包含intstr值,因此不能突变。因此,它可以接受这些值来绘制

产量

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.