我有以下情节:
我的熊猫数据集使用多索引熊猫,如
下面是我的代码:
ax = plt.gca()
df['adjClose'].plot(ax=ax, figsize=(12,4), rot=9, grid=True, label='price', color='orange')
df['ma5'].plot(ax=ax, label='ma5', color='yellow')
df['ma100'].plot(ax=ax, label='ma100', color='green')
# df.plot.scatter(x=df.index, y='buy')
x = pd.to_datetime(df.unstack(level=0).index, format='%Y/%m/%d')
# plt.scatter(x, df['buy'].values)
ax.scatter(x, y=df['buy'].values, label='buy', marker='^', color='red')
ax.scatter(x, y=df['sell'].values, label='sell', marker='v', color='green')
plt.show()
.csv
的数据symbol,date,close,high,low,open,volume,adjClose,adjHigh,adjLow,adjOpen,adjVolume,divCash,splitFactor,ma5,ma100,buy,sell
601398,2020-01-01 00:00:00+00:00,5.88,5.88,5.88,5.88,0,5.2991971571,5.2991971571,5.2991971571,5.2991971571,0,0.0,1.0,,,,
601398,2020-01-02 00:00:00+00:00,5.97,6.03,5.91,5.92,234949400,5.3803073177,5.4343807581,5.3262338773,5.3352461174,234949400,0.0,1.0,,,,
601398,2020-01-03 00:00:00+00:00,5.99,6.02,5.96,5.97,152213050,5.3983317978,5.425368518,5.3712950777,5.3803073177,152213050,0.0,1.0,,,,
601398,2020-01-06 00:00:00+00:00,5.97,6.05,5.95,5.96,226509710,5.3803073177,5.4524052382,5.3622828376,5.3712950777,226509710,0.0,1.0,,,,
上面的数据是我保存csv后的数据,但在重新加载后,它丢失了原始结构,如下所示
tuple
的形式显示。散点图是根据datetime
值x
绘制的,该值不是ax
轴上的值,因此它们绘制在最右侧。tuples
,如x
表示'symbol'
在多索引中的级别。'symbol'
后,使用df.index = pd.to_datetime(df.index).date
将'date'
转换为datetime dtype
'symbol'
,因此应该为每个数据帧绘制一个单独的图李>pandas 1.3.1
、python 3.8
和matplotlib 3.4.2
data.high
和{df
可以通过以下方式方便地创建:只要找到另一种方法来解决我的问题:
这对我来说是一项考验
我认为类似于@Trentons last Advice上的贝娄:
相关问题 更多 >
编程相关推荐