【问题标题】:Plotting sized circles with python in pandas在 pandas 中用 python 绘制大小的圆圈
【发布时间】:2018-03-21 19:16:44
【问题描述】:

这是我的数据示例:

import pandas as pd
mydic = {'user1': {'product': 234, 'device': 567}, 'user1': {'product': 678, 'device': 256},'user2': {'product': 890, 'device': 456},'user2': {'product': 256, 'device': 678},'user3': {'product': 456, 'device': 278}}
df = pd.DataFrame.from_dict(mydic, orient='index', dtype=None)
df.head()

我可以绘制散点图或条形图,但对于这个特定的任务,我相信,最好的方法是显示每个值大小的圆圈,并为每个用户提供注释。像这样的:

我什至不确定在 python 中是否可行。任何人都可以提出一些聪明的建议吗?

【问题讨论】:

  • 如果你用谷歌搜索"draw circle python",你会发现如何在python中画一个圆。我们还能为您提供什么帮助?
  • 我知道这不是对您的具体问题的回答(因此是评论),但我相信条形图比圆圈更好。很容易看到两倍大的圆圈,而实际上它是原来的 4 倍。你可以使用 seaborn:seaborn.pydata.org/examples/color_palettes.html 不然画圈不是很困难。

标签: python-2.7 pandas matplotlib


【解决方案1】:

可能不是最优雅的解决方案,但请尝试:

%matplotlib inline
import matplotlib.pyplot as plt
n = len(df)
y = range(n)
scale = 5
f, ax = plt.subplots(1, 1)
ax.scatter([[1]] * n, y, s=scale * df['product'])
for i in range(n):
    ax.text(2, y[i], df.index[i])
ax.scatter([[3]] * n, y, s=scale * df['device'])
ax.set_xlim(0, 4)
ax.set_ylim(-1, n);

使用样式以获得更好的输出(您可以移除轴和网格,更改大小和颜色):

【讨论】:

  • 谢谢!这正是我想要的。
猜你喜欢
  • 2012-09-25
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 2014-04-11
  • 2012-09-19
相关资源
最近更新 更多