【问题标题】:Plot Frequency of Values of Multiple Columns绘制多列值的频率
【发布时间】:2020-10-13 21:45:58
【问题描述】:

我想创建两列中值出现频率的熊猫图。散点图将包含一条回归线。结果是带有回归线的类似热图的图。

【问题讨论】:

  • 我已经发布了一个答案,但我希望前三行的答案更像熊猫。请注意,我要的是熊猫情节答案。

标签: pandas plot count regression frequency


【解决方案1】:

首先,将列“A”和“B”组合成一个唯一值。在这种情况下,两列都是数字,所以我使用加法。接下来使用 value_counts 创建频率。使用 pandas 散点图创建散点图/气泡图/热图。最后使用 numpy.polyfit 删除回归线。

combined = (plotdf['A']*plotdf['B'].nunique()+plotdf['B']) # combine numeric values of columns A and B
vcounts = combined.value_counts() # get value counts of combined values
frequency = combined.map(vcounts) # lookup count for each row
plt = plotdf.plot(x='A',y='B',c=frequency,s=frequency,colormap='viridis',kind='scatter',figsize=(16,8),title='Frequency of A and B')
plt.set(xlabel='A',ylabel='B')
x = plotdf['A'].values
y = plotdf['B'].values
m, b = np.polyfit(x, y, 1) # requires numpy
plt.plot(x, m*x + b, 'r') # r is color red

【讨论】:

  • 啊,我现在看到我刚刚重新发明了一个 hexbin 情节。
猜你喜欢
  • 2021-08-23
  • 2018-01-03
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 2021-12-28
  • 2019-05-26
  • 1970-01-01
  • 2014-04-13
相关资源
最近更新 更多