python - 熊猫散点图,时间数据和点大小
问题描述
我的 DataFrame 看起来:
我用这段代码绘制它:
tmp['event_name'].plot(style='.', figsize=(20,10), grid=True)
我想更改点的大小(使用列详细信息)。问题:我该怎么做?绘图没有参数大小,我不能使用 plot.scatter() 因为我不能使用 x 轴的时间格式。
解决方案
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()
推荐阅读
- php - PHP Laravel - MSSQL - WhereBetween 和 Where
- java - 如何将长(4字节)值写入android中的二进制文件
- javascript - 如何重置 Jasmine Spy 对象?
- mongodb - PyMongo - 查找包含唯一键的文档并删除该键及其内容
- postgresql - PostgreSQL COPY 方法需要什么锁?
- laravel - PayTm 交易状态 API
- r - R devtools 无法安装 - Ubuntu 20.04 - 'pkgload' 的包或命名空间加载失败
- html - 如何从我的 HTML/CSS 项目中删除这些不需要的行?
- reactjs - 如何从父网页拖放到 iFrame?
- python - 计划每周更新