将价格作为非零体积值的水平线绘制

时间:2019-02-10 06:15:10

标签: python python-3.x pandas matplotlib

我的代码:

import matplotlib.pyplot as plt
plt.style.use('seaborn-ticks')
import pandas as pd
import numpy as np

path = 'C:\\File\\Data.txt'
df = pd.read_csv(path, sep=",")
df.columns = ['Date','Time','Price','volume']
df = df[df.Date == '08/02/2019'].reset_index(drop=True)
df['Volume'] = np.where((df.volume/1000) < 60, 0, (df.volume/1000))

df.plot('Time','Price')

dff = df[df.Volume > 60].reset_index(drop=True)
dff = dff[['Date','Time','Price','Volume']]
print(dff)

plt.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95, wspace=None, hspace=None)
plt.show()

我的绘图输出如下:

Current_Output

dff Datframe的输出如下:

     Date      Time   Price  Volume
0  08/02/2019  13:39:43  685.35    97.0
1  08/02/2019  13:39:57  688.80    68.0
2  08/02/2019  13:43:50  683.00    68.0
3  08/02/2019  13:43:51  681.65    92.0
4  08/02/2019  13:49:42  689.95    70.0
5  08/02/2019  13:52:00  695.20    64.0
6  08/02/2019  14:56:42  686.25    68.0
7  08/02/2019  15:03:15  685.35    63.0
8  08/02/2019  15:03:31  683.15    69.0
9  08/02/2019  15:08:08  684.00    61.0

我想根据下图以垂直线的形式绘制此表的价格。任何帮助。

Desired_Result

2 个答案:

答案 0 :(得分:1)

根据您的图像,我认为您的意思是水平线。无论哪种方式都非常简单,Pyplot具有hlines / vlines内置函数。就您而言,尝试类似

plt.hlines(dff['Price'], '08/02/2019', '09/02/2019') 

答案 1 :(得分:0)

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

path = 'File.txt'
df = pd.read_csv(path, sep=",")
df.columns = ['Date','Time','Price','volume']
df = df[df.Date == '05/02/2019'].reset_index(drop=True)
df['Volume'] = np.where((df.volume/7500) < 39, 0, (df.volume/7500))
df["Time"] = pd.to_datetime(df['Time'])

df.plot(x="Time",y='Price', rot=0)

plt.title("Date: " + str(df['Date'].iloc[0]))

dff = df[df.Volume > 39].reset_index(drop=True)
dff = dff[['Date','Time','Price','Volume']]
print(dff)

dict = dff.to_dict('index')
for x in range(0, len(dict)):
    plt.axhline(y=dict[x]['Price'],linewidth=2, color='blue')

plt.subplots_adjust(left=0.05, bottom=0.06, right=0.95, top=0.96, wspace=None, hspace=None)
plt.show()