首页 > 解决方案 > 熊猫散点图,时间数据和点大小

问题描述

我的 DataFrame 看起来:

在此处输入图像描述

我用这段代码绘制它:

tmp['event_name'].plot(style='.', figsize=(20,10), grid=True)

结果看起来: 在此处输入图像描述

我想更改点的大小(使用列详细信息)。问题:我该怎么做?绘图没有参数大小,我不能使用 plot.scatter() 因为我不能使用 x 轴的时间格式。

标签: pythonpandasmatplotlib

解决方案


DataFrame.plot passes any unknown keywords down to Matplotlib.Artist, as stated in the linked docs. Therefore, you can specify the marker size using the general matplotlib syntax ms:

tmp['event_name'].plot(style='.', figsize=(20,10), grid=True, ms=5)

That said, you can use plt.scatter with time stamps as well, which makes using the 'details' column as marker size more straight forward:

import matplotlib.pyplot as plt
import pandas as pd
data = {'time':       ['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04'],
        'event_name': [2, 2, 2, 2],
        'details':    [46, 16, 1, 7]}
df = pd.DataFrame(data)
dates = [pd.to_datetime(date) for date in df.time]
plt.scatter(dates, df.event_name, s=df.details)
plt.show()

推荐阅读