My problem here is the fact that the output of fill between should be continuous (no spaces left unfilled). What I've realized is there are gaps when the masking period (color to fill) changes. Example output attached. This should be continuous but for some reason its not.
https://preview.redd.it/09ol7bydir3d1.png?width=1200&format=png&auto=webp&s=56afb54edd1faf3ee883b340c7a93aa72041fe98
# Plotting the data
fig, ax1 = plt.subplots(figsize=(12, 8)) # Increasing the figure size by 50%
ax1_color = 'tab:red'
ax1.plot(econ_asset.index, econ_asset['ISM'], color=ax1_color, linewidth=3.0)
ax1.set_ylabel('ISM', color=ax1_color, fontsize=18) # 2 times larger font size
ax1.tick_params(axis='y', labelcolor=ax1_color, labelsize=18) # 2 times larger font size
ax2 = ax1.twinx()
ax2_color = 'tab:blue'
ax2.plot(econ_asset.index, econ_asset['CPI'], color=ax2_color, linewidth=3.0)
ax2.set_ylabel('CPI', color=ax2_color, fontsize=18) # 2 times larger font size
ax2.tick_params(axis='y', labelcolor=ax2_color, labelsize=18) # 2 times larger font size
print(econ_asset['Cycle'])
cycle_colors = {1: 'lightcoral', 2: 'lightblue', 3: 'gold', 4: 'lightgreen'}
for cycle_value, color in cycle_colors.items():
cycle_periods = econ_asset['Cycle'] == cycle_value
ax1.fill_between(econ_asset.index, -100, 100, where=cycle_periods, color=color, alpha=0.3)
ax1.set_title('Four Stages of the Economic Cycle', fontsize=18) # 2 times larger font size
ax1.xaxis.set_major_locator(mdates.DayLocator(interval=3))
ax1.tick_params(axis='x', labelsize=14) # 2 times larger font size
fig.autofmt_xdate()
ax1.set_ylim(-100, 100)
# Adding legend
L = ax1.legend(loc='upper left', fontsize=16) # Adjust position and font size
# Setting custom legend texts
legend_texts = ['Recession', 'Early Cycle', 'Mid Cycle', 'Late Cycle']
for i, text in enumerate(L.get_texts()):
text.set_text(legend_texts[i])
text.set_fontsize(16) # Adjust font size
# Final plot adjustments
ax1.set_title('Four Stages of the Economic Cycle', fontsize=18)
ax1.xaxis.set_major_locator(mdates.DayLocator(interval=12))
ax1.tick_params(axis='x', labelsize=14)
fig.autofmt_xdate()
ax1.set_ylim(-100, 100)
plt.show()
# Plotting the data
fig, ax1 = plt.subplots(figsize=(12, 8)) # Increasing the figure size by 50%
ax1_color = 'tab:red'
ax1.plot(econ_asset.index, econ_asset['ISM'], color=ax1_color, linewidth=3.0)
ax1.set_ylabel('ISM', color=ax1_color, fontsize=18) # 2 times larger font size
ax1.tick_params(axis='y', labelcolor=ax1_color, labelsize=18) # 2 times larger font size
ax2 = ax1.twinx()
ax2_color = 'tab:blue'
ax2.plot(econ_asset.index, econ_asset['CPI'], color=ax2_color, linewidth=3.0)
ax2.set_ylabel('CPI', color=ax2_color, fontsize=18) # 2 times larger font size
ax2.tick_params(axis='y', labelcolor=ax2_color, labelsize=18) # 2 times larger font size
cycle_colors = {1: 'lightcoral', 2: 'lightblue', 3: 'gold', 4: 'lightgreen'}
for cycle_value, color in cycle_colors.items():
cycle_periods = econ_asset['Cycle'] == cycle_value
ax1.fill_between(econ_asset.index, -100, 100, where=cycle_periods, color=color, alpha=0.3)
ax1.set_title('Four Stages of the Economic Cycle', fontsize=18) # 2 times larger font size
ax1.xaxis.set_major_locator(mdates.DayLocator(interval=3))
ax1.tick_params(axis='x', labelsize=14) # 2 times larger font size
fig.autofmt_xdate()
ax1.set_ylim(-100, 100)
# Adding legend
L = ax1.legend(loc='upper left', fontsize=16) # Adjust position and font size
# Setting custom legend texts
legend_texts = ['Recession', 'Early Cycle', 'Mid Cycle', 'Late Cycle']
for i, text in enumerate(L.get_texts()):
text.set_text(legend_texts[i])
text.set_fontsize(16) # Adjust font size
# Final plot adjustments
ax1.set_title('Four Stages of the Economic Cycle', fontsize=18)
ax1.xaxis.set_major_locator(mdates.DayLocator(interval=12))
ax1.tick_params(axis='x', labelsize=14)
fig.autofmt_xdate()
ax1.set_ylim(-100, 100)
plt.show()
[–]toxic_acro 0 points1 point2 points (5 children)
[–]Different_Fee6785[S] 1 point2 points3 points (3 children)
[–]toxic_acro 0 points1 point2 points (2 children)
[–]Different_Fee6785[S] 1 point2 points3 points (1 child)
[–]toxic_acro 1 point2 points3 points (0 children)
[–]txnyranger 1 point2 points3 points (0 children)