我正在尝试从熊猫数据框中构建一个简单的散点图,但是它会生成颜色完全相同的散点图。我检查了它是否适用于随机的熊猫数据框。
这是MWE。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cbook
import matplotlib as mpl
import matplotlib.cm as cm
#This dataframe works perfectly well.
df = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=list('CRD'))
#Here is a dataframe converted to list, that doesn't work.
df['C'] = [4.0, 4.0, 4.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 16.0, 16.0, 16.0, 16.0, 16.0, 16.0, 16.0]
df['D'] = [1.2, 1.6, 2.0, 1.2, 1.6, 1.6, 1.6, 1.6, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
df['R'] = [100.0, 100.0, 100.0, 100.0, 100.0, 1400.0, 1800.0, 2200.0, 100.0, 1400.0, 1800.0, 2200.0, 1000.0, 1500.0, 1700.0, 1900.0, 2200.0, 2500.0, 2800.0]
#My first hunch was that the column 'D' is a string but the dtypes returns it as a float64.. what am I missing?
#C float64
#D float64
#R float64
fig = plt.figure()
ax1 = plt.subplot(1,1,1)
im = ax1.scatter(df['C'], df['R'] , c=df['D'])
cb = fig.colorbar(im, ax=ax1)
cb.set_label('D')
ax1.set_ylabel('R')
ax1.set_xlabel('C')
fig.tight_layout()
plt.show()
plt.close()
答案 0 :(得分:0)
您的分散点相互覆盖,因为大多数(df['C'],df['R'])
对都是多重定义的。
print(df.groupby(['C','R','D']).apply(list))
收益
C R D
4.0 100.0 1.2 [C, R, D]
1.6 [C, R, D]
2.0 [C, R, D]
8.0 100.0 1.2 [C, R, D]
1.6 [C, R, D]
2.0 [C, R, D]
1400.0 1.6 [C, R, D]
2.0 [C, R, D]
1800.0 1.6 [C, R, D]
2.0 [C, R, D]
2200.0 1.6 [C, R, D]
2.0 [C, R, D]
16.0 1000.0 2.0 [C, R, D]
1500.0 2.0 [C, R, D]
1700.0 2.0 [C, R, D]
1900.0 2.0 [C, R, D]
2200.0 2.0 [C, R, D]
2500.0 2.0 [C, R, D]
2800.0 2.0 [C, R, D]