我正在尝试使用 python 3.7.4 中的 plotnine 0.6.0 创建一个简单的绘图,其中的线条根据因子变量着色。
import pandas as pd
import plotnine as pn
import datetime
# data
df = pd.DataFrame(
{'name': ('Eric', 'Eric', 'Eric', 'Eric', 'Eric', 'Eric', 'Nico', 'Nico',
'Nico', 'Nico', 'Nico', 'Nico', 'Sanne', 'Sanne', 'Sanne', 'Sanne',
'Sanne', 'Sanne'),
'date': (datetime.date(2013, 8, 15), datetime.date(2013, 8, 15),
datetime.date(2013, 8, 15), datetime.date(2013, 8, 16),
datetime.date(2013, 8, 16), datetime.date(2013, 8, 16),
datetime.date(2013, 8, 15), datetime.date(2013, 8, 15),
datetime.date(2013, 8, 15), datetime.date(2013, 8, 16),
datetime.date(2013, 8, 16), datetime.date(2013, 8, 16),
datetime.date(2013, 8, 15), datetime.date(2013, 8, 15),
datetime.date(2013, 8, 15), datetime.date(2013, 8, 16),
datetime.date(2013, 8, 16), datetime.date(2013, 8, 16)),
'altitude': ( 71, 68, 68, 92, 95, 104, 382, 197, 206, 157, 156, 157, 55,
54, 55, 65, 62, 73)
})
# summarize the data by date
summ = df.groupby(['name', 'date']).altitude.mean().reset_index(name = 'altitude')
# plot the data by "name"
pn.ggplot(mapping = pn.aes(x = 'date',
y = 'altitude',
color = 'name'),
data = summ) +\
pn.geom_line()
但抛出错误:
C:\Anaconda3\lib\site-packages\plotnine\geoms\geom_path.py:83:
PlotnineWarning: geom_path: Each group consist of only one observation.
Do you need to adjust the group aesthetic?
"group aesthetic?", PlotnineWarning)
如果我删除颜色方面,
pn.ggplot(mapping = pn.aes(x = 'date',
y = 'altitude'),
data = summ) +\
pn.geom_line()
我得到:
我知道我的问题与此有关,但我不想要 1 行。我想为每个name
.