如何在Xaxis(股市午休)中跳过某段时间

2024-04-27 01:09:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码来绘制图表。 股市午休时间是12:00-13:00

你可以看到这张图有点难看,因为午休时间有一个缺口。 如何在X轴上跳过12:00-13:00,以便Y轴数据是连续的,并且图形中没有这样的间隙?你知道吗

enter image description here

import pandas as pd
import pandas_datareader.data
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import HourLocator
import datetime

today_s = datetime.date.today().strftime('%Y%m%d')

df = pd.read_csv('futures-sample.txt', names=['Time', 'HSIF', 'Volume'], delim_whitespace=True)
df['Time'] = pd.to_datetime(df['Time'])

# RSI
window_length = 14
df['Delta'] = df['HSIF'].diff().shift(0)
df['DeltaUp'] = df['Delta'].apply(lambda x: x if x > 0 else 0)
df['DeltaDown'] = df['Delta'].apply(lambda x: -x if x < 0 else 0)
df['RollUp'] = df['DeltaUp'].rolling(window_length).mean()
df['RollDown'] = df['DeltaDown'].rolling(window_length).mean()
df['RS'] = df['RollUp'] / df['RollDown']
df['RSI'] = 100.0 - (100.0 / (1.0 + df['RS']))
pd.set_option('display.max_rows', 10000)

df = df.set_index('Time')

fig , ax1 = plt.subplots()
ax1.set_xlabel('Time')
ax1.set_ylabel('HSIF', color='blue')
ax1.plot(df['HSIF'], color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
ax2= ax1.twinx()
ax2.set_ylabel('RSI', color='orchid')
ax2.plot(df['RSI'], color='orchid')
ax2.tick_params(axis='y', labelcolor='orchid')

fig = ax2.get_figure()
fig.set_size_inches(10, 7)
fig.savefig('/var/www/html/temp.png', dpi=100)

以下是未来的内容-示例.txt你知道吗

# cat futures-samples.txt
2019/05/16-09:15 27830 2031
2019/05/16-09:16 27815 995
2019/05/16-09:17 27829 961
2019/05/16-09:18 27848 663
2019/05/16-09:19 27873 869
2019/05/16-09:20 27847 854
2019/05/16-09:21 27828 784
...
2019/05/16-11:52 28087 175
2019/05/16-11:53 28076 346
2019/05/16-11:54 28089 223
2019/05/16-11:55 28096 137
2019/05/16-11:56 28102 175
2019/05/16-11:57 28110 294
2019/05/16-11:58 28089 256
2019/05/16-11:59 28089 235
2019/05/16-12:59 28070 108
2019/05/16-13:00 28061 800
2019/05/16-13:01 28070 470
2019/05/16-13:02 28051 326
2019/05/16-13:03 28058 699
2019/05/16-13:04 28059 296
2019/05/16-13:05 28064 369
2019/05/16-13:06 28046 683
2019/05/16-13:07 28051 457
2019/05/16-13:08 28049 340

Tags: importtxtdfdatetimetimeasfigwindow
1条回答
网友
1楼 · 发布于 2024-04-27 01:09:43

我认为这应该管用,但我不确定这是不是最好的方法。你知道吗

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import HourLocator
import datetime

today_s = datetime.date.today().strftime('%Y%m%d')

df = pd.read_csv(r'test.txt', names=['Time', 'HSIF', 'Volume'], delim_whitespace=True)

df['Time'] = pd.to_datetime(df['Time'])
df["Time"] = df['Time'].apply(lambda x: x.strftime('%d %H:%M'))
# RSI
window_length = 14
df['Delta'] = df['HSIF'].diff().shift(0)
df['DeltaUp'] = df['Delta'].apply(lambda x: x if x > 0 else 0)
df['DeltaDown'] = df['Delta'].apply(lambda x: -x if x < 0 else 0)
df['RollUp'] = df['DeltaUp'].rolling(window_length).mean()
df['RollDown'] = df['DeltaDown'].rolling(window_length).mean()
df['RS'] = df['RollUp'] / df['RollDown']
df['RSI'] = 100.0 - (100.0 / (1.0 + df['RS']))
pd.set_option('display.max_rows', 10000)

#df = df.set_index('Time')
#print (type(df['Time']))
fig , ax1 = plt.subplots()
ax1.set_xlabel('Time')
#ax1.set_xticklabels(df['Time'])
ax1.set_ylabel('HSIF', color='blue')
ax1.plot(df['Time'], df['HSIF'], color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
ax2= ax1.twinx()
ax2.set_ylabel('RSI', color='orchid')
ax2.plot(df['Time'], df['RSI'], color='orchid')
ax2.tick_params(axis='y', labelcolor='orchid')

# set xtick interval
tick_start, tick_end = ax1.get_xlim()
ax1.xaxis.set_ticks(np.arange(tick_start, tick_end, 2))

fig = ax2.get_figure()
fig.set_size_inches(10, 7)
plt.show()

相关问题 更多 >