Python Forum

Full Version: How to preserve x-axis labels despite deleted subplot?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 23 11:59:48 2020

@author: drkle
"""
import matplotlib.pyplot as plt
import collections

markets = ['ES', 'GC', 'CL', 'EC', 'US']

axL_index = collections.OrderedDict()
axR_index = collections.OrderedDict()
axL_index[1] = 0; axL_index[2] = 0; axL_index[3] = 1
axL_index[4] = 1; axL_index[5] = 2; axL_index[6] = 2
axR_index[1] = 0; axR_index[2] = 1; axR_index[3] = 0
axR_index[4] = 1; axR_index[5] = 0; axR_index[6] = 1

fig, ax = plt.subplots(3,2,sharex=True)
if len(markets) % 2 != 0:
    fig.delaxes(ax[axL_index[next(reversed(axL_index))],axR_index[next(reversed(axR_index))]])
fig.autofmt_xdate()
This deletes the [sixth] subplot in the bottom right, but it takes the x-axis labels in this column along with it. I shared the x-axis labels because the x-axis (time) is the same for all six plots and they only need to be shown at the bottom of every column. How can I preserve that--or do they somehow need to be redrawn below the subplot in row 1 (zero-indexed) column 1?
This works:
import matplotlib.pyplot as plt
import collections

markets = ['ES', 'GC', 'CL', 'EC', 'US']

axL_index = collections.OrderedDict()
axR_index = collections.OrderedDict()
axL_index[1] = 0; axL_index[2] = 0; axL_index[3] = 1
axL_index[4] = 1; axL_index[5] = 2; axL_index[6] = 2
axR_index[1] = 0; axR_index[2] = 1; axR_index[3] = 0
axR_index[4] = 1; axR_index[5] = 0; axR_index[6] = 1

fig, axs = plt.subplots(3,2)
for a in range(1,7):
    axs[axL_index[a],axR_index[a]].tick_params(axis='x', rotation=45)

if len(markets) % 2 != 0:
    fig.delaxes(axs[axL_index[next(reversed(axL_index))],axR_index[next(reversed(axR_index))]])
fig.tight_layout()
I removed the sharex=True.

I removed the previous Line 22.

It's frustrating how many webpages I had to go through (including SO, matplotlib documentation itself--none from this forum) to figure this out. Lots of references to attributes that (according to my IDE) don't exist, maybe deprecated syntax and/or methods, and even confusion (seemingly) between plt (state-based) and ax/fig (object-based) paradigms... I guess this is just the nature of the beast.