0

I have some data which I have plotted like that below. I want to take the area which I have marked in a purple box and exclude it from being plotted, basically, so that subplot 1's x axis is the interval of the sort like [1981, 1993] + [2009, 2014].

The code I use to do this is below:

# do figure 1
fig1, ax1 = plt.subplots(nrows=2, figsize=(8.5, 11))

# > plot 1
ax1[0].set_title('plot1')
ax1[0].set_xlabel('x')
ax1[0].set_ylabel('y1', color='tab:red')
ax1[0].plot(cumlassets.index, cumlassets.values, color='tab:red')
ax1[0].tick_params(axis='y', labelcolor='tab:red')

# > plot2
ax1a = ax1[0].twinx()
ax1a.set_ylabel('y2', color='tab:blue')
ax1a.plot(ail_2016.index, ail_2016.values, color='tab:blue')
ax1a.tick_params(axis='y', labelcolor='tab:blue')

# > s
sns.barplot(x='x', y='y', hue='hue', dodge=False, data=res, ax=ax1[1])
ax1[1].set_title('plot2')
ax1[1].set_xticklabels(res['x'].values, rotation='vertical', ha='center')
ax1[1].set_xlabel('x')
ax1[1].set_ylabel('y')

fig1.tight_layout()

I'll note that the subplot on top already does some funky ax[0].twinx() stuff to permit the plotting of the two lines on the same plot with different scales.

It'd also be fantastic to find a way to have this done on the bottom bar plot, which wants to do something similar. The way I have it set up right now is a bit hacky, with an intermediate point in the data with {'x': '. . .', 'y': 0}.

enter image description here

EDIT: Regarding the bar plot, I've decided to just suppress the ticks.

ifly6
  • 5,003
  • 2
  • 24
  • 47
  • Something like [this](https://stackoverflow.com/questions/32185411/break-in-x-axis-of-matplotlib)? – ImportanceOfBeingErnest Sep 30 '19 at 17:39
  • I've not found a way to have that along with the two independent scales on both sides as well as in subplots. On top of that, as the x-axis has different lengths on both sides, I've not seen a corrected scaling which doesn't distort the hacked-in slashes. – ifly6 Sep 30 '19 at 17:54
  • The simple way is to just have two subplots. If you want the axes to have the correct relative widths, you can do that easily with something like `fig, axs = subplots(1, 2, gridspec_kw={'width_ratios': [nyears[0]/totyears, nyears[1]/totyears]})` – Jody Klymak Sep 30 '19 at 23:42
  • I've tried that, when you try to add in slashes, they don't maintain consistent angle. Nor can I find an easy way to reduce the horizontal separation between the two subplots – ifly6 Sep 30 '19 at 23:59
  • Not sure what you mean by "consistent angle". To reduce the space between the subplots you can use 'wspace=small_number' in the grid spec kwargs. https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.gridspec.GridSpec.html#matplotlib.gridspec.GridSpec – Jody Klymak Oct 01 '19 at 04:59

0 Answers0