我有这个数据
df = pd.read_csv('https://docs.google.com/spreadsheets/d/e/2PACX-1vQxY-rTQNBbMWtRI2p8m_gj0TvmkHt3_CqNkhRILq6s5xL3mGX-IXtZt9pej-ae3ZNN1EnAP_iC0unx/pub?output=csv', index=False)
df.head()
Id Year Period Cumulative YearPeriod YearPeriodStr
0 16432976 1 1 6.0 11 Period 11
1 16432976 1 2 7.0 12 Period 12
2 16432976 1 0 0.0 10 Period 10
3 16879454 1 1 6.0 11 Period 11
4 16879454 1 2 6.0 12 Period 12
我想要实现的是 x 轴上两个序数变量的组合。第一级应该是年,第二个时期。以下代码是我认为会导致我想要的图表的内容:
%opts Curve [width=600 height=400 show_grid=True default_tools=['save','hover',] show_legend=False] (color='indianred', alpha=0.3, line_width=0.2, )
ds = hv.Dataset(df).sort(['Year','Period'])
ds.to(hv.Curve, ['Year','Period'],'Cumulative', 'Id').overlay()
但这会将 Period 放在 y 轴上。下面的代码让我更接近我想要的:
%opts Curve [width=600 height=400 show_grid=True default_tools=['save','hover',] show_legend=False] (color='indianred', alpha=0.3, line_width=0.2, )
# Numerical column
df['YearPeriod'] = (df.Year * 10) + (df.Period)
ds = hv.Dataset(df).sort(['Year','Period'])
ds.to(hv.Curve, 'YearPeriod','Cumulative', 'Id').overlay()
但是 x 轴现在是一个连续变量,而我想要的是第二级,周期,是序数。以下代码或多或少会导致我想要的,但是当行数超过 120 时会失败。
%opts Curve [width=600 height=400 show_grid=True default_tools=['save','hover',] show_legend=False] (color='indianred', alpha=0.3, line_width=0.2, )
# String
df['YearPeriodStr'] = 'Period'+ df['Year'].astype(str) + df['Period'].astype(str)
ds = hv.Dataset(df.head(120)).sort(['Year','Period'])
ds.to(hv.Curve, 'YearPeriodStr','Cumulative', 'Id').overlay()
我错过了什么?