通过matplotlib图表绘制熊猫数据框并在图表上绘制点

时间:2019-08-07 11:12:50

标签: python pandas dataframe matplotlib

我试图仅从matplotlib图表上的数据框C列出BUY,SELL,BCLOSE和SCLOSE点(即SIT或SHRTCLS等),该点可以在下方生成

例如。在B列(5.53)的第二个价格点,因为这是买入,因此我试图将此点绘制在图形的直线上。 B行中的第4个价格为BCLOSE,也应将其添加到图表中,依此类推。

感谢您的时间。

import pandas as pd
import matplotlib.pyplot as plt

 df = pd.DataFrame()
df['A'] = ('11/06/2019','10/06/2019','9/06/2019','8/06/2019','7/06/2019','6/06/2019','5/06/2019','4/06/2019','3/06/2019','2/06/2019','1/06/2019','31/05/2019','30/05/2019')
df['B'] = (5.97,5.53,5.13,4.85,4.87,4.92,4.9,5.66,5.66,5.72,5.72,5.68,6.05)
df['C'] = ('BHODL','BUY','SIT','BCLOSE','BUY','SIT','SELL','SIT','SIT','SIT','SHRTCLS','BCLSHRT','SELL')
print(df)

ax = df.plot(title='tesing123')
ax.set_xlabel('date')
ax.set_ylabel('price')
ax.grid()
plt.show()

2 个答案:

答案 0 :(得分:1)

使用-

ax = df[df['C'].isin(['BUY','SELL','BCLOSE','SCLOSE'])].plot(title='tesing123')
ax.set_xlabel('date')
ax.set_ylabel('price')
ax.grid()
plt.show()

输出

enter image description here

答案 1 :(得分:0)

尝试使用此代码:

z = df['A']
y = df['B']
n = df['C']

ax = df.plot(title='tesing123')

ax.scatter(z, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (z[i], y[i]))

ax.grid()

plt.show()