python - 如何在X轴上跳过特定时间段(股市午休时间)

标签 python matplotlib

我使用以下代码来绘制图表。 股市午休时间为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)

这是 futures-sample.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

最佳答案

我认为它应该有效,但我不确定这是否是最好的方法。

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()

关于python - 如何在X轴上跳过特定时间段(股市午休时间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56253070/

相关文章:

javascript - 使用 Ajax 驱动的进度条上传 Django 文件

python - 如何显示每个 matplotlib 子图的 x 轴标签

python - 组合两个字典,其中字典一个键是整数范围,第二个字典键是可能落入第一个键范围的整数

python - 逐行读取文件时出现名称未定义错误python

python - 无法启动spyder ide,无法在动态链接库中找到序数242 [mkl_intel_thread.dlll]

python - 如何使用 Python Selenium 获取多个元素的文本内容?

python - Seaborn.despine() 消除将 y 轴移动到图右侧的效果

python - matplotlib 中的 rc 键列表。刻度标签旋转

python - Matplotlib yaxis范围显示使用绝对值而不是偏移值?

python - 使用 TeX 时 EngFormatter 中的错误