gpt4 book ai didi

python - 如何从 Pandas Dataframe 创建事件图(如 Github 贡献图)

转载 作者:太空狗 更新时间:2023-10-30 00:40:36 25 4
gpt4 key购买 nike

你们都知道这张图:

enter image description here

您有一周中的某一天和一年中的一个月进行某种事件。假设我有这个 Pandas 数据框:

In [87]: metadf2[['Week','Activity']]
Out[87]:
Week Activity
weekday
0 15 1.6
0 15 1.1
0 17 0.6
0 17 0.8
0 17 1.3
0 17 2.6
0 17 0.9
0 19 1.0
0 19 8.0
0 19 1.6
0 23 5.0
0 23 1.2
0 23 0.6
0 23 5.6
1 15 1.6
1 15 0.2
1 15 0.1
1 15 0.1
1 15 0.4
1 17 12.2
1 19 10.2
1 19 1.6
2 13 1.7
2 14 0.0
2 14 0.0
2 15 6.9
2 15 2.5
2 15 5.5
2 17 6.2
2 17 1.3
... ... ...
3 14 1.1
3 14 4.9
3 14 4.0
3 14 1.5
3 14 3.9
4 14 0.2
5 15 5.4
5 15 5.1
5 18 9.5
5 18 8.8
5 20 108.8
5 20 11.1
5 20 11.2
6 13 74.9
6 13 2.0
6 13 3.2
6 13 2.0
6 13 16.7
6 13 5.5
6 16 0.4
6 15 7.6
6 15 11.7
6 15 25.8
6 16 0.4
6 16 0.4
6 16 1.3
6 20 2.0
6 20 20.5
6 20 77.0
6 20 32.8

如何使用 Matplotlib 创建这样一个 Github-Activity-like 图表?我认为 contour plot with nearest是在正确的方向,不是吗?

最佳答案

这是一个供您开始的示例:

import pylab as pl
import numpy as np
import pandas as pd

# prepare some random data
N = 100
np.random.seed(0)
weekday = np.random.randint(0, 7, N)
week = np.random.randint(0, 40, N)
activity = np.random.randint(0, 100, N)

df = pd.DataFrame({"weekday":weekday, "week":week, "activity":activity})
df.drop_duplicates(subset=["weekday", "week"], inplace=True)

# reshape the data and plot it
df2 = df.pivot(columns="week", index="weekday", values="activity")
df2.fillna(0, inplace=True)

Weekday, Week = np.mgrid[:df2.shape[0]+1, :df2.shape[1]+1]
fig, ax = pl.subplots(figsize=(12, 4))
ax.set_aspect("equal")
pl.pcolormesh(Week, Weekday, df2.values, cmap="Greens", edgecolor="w", vmin=-10, vmax=100)
pl.xlim(0, df2.shape[1])

输出:

enter image description here

关于python - 如何从 Pandas Dataframe 创建事件图(如 Github 贡献图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24163313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com