Python Forum
How to preserve x-axis labels despite deleted subplot?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to preserve x-axis labels despite deleted subplot?
#1
# -*- 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?
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot remove one of the x-ticks from a 2x1 subplot. generalzu10 1 188 Mar-20-2024, 04:24 AM
Last Post: generalzu10
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 850 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  python-docx: preserve formatting when printing lines Tmagpy 4 2,005 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
  How to avoid the extra set of y-axis labels? Mark17 9 2,244 May-17-2022, 06:26 PM
Last Post: Mark17
  Floor division problem with plotting x-axis tick labels Mark17 5 2,047 Apr-03-2022, 01:48 PM
Last Post: Mark17
  x-axis labels with Matplotlib Mark17 8 2,124 Mar-23-2022, 06:10 PM
Last Post: Mark17
  deleted Overdue 0 1,115 Dec-14-2021, 06:57 PM
Last Post: Overdue
  Subplot - Plotting 4 plots on the same row Menthix 1 1,391 Nov-07-2021, 09:03 PM
Last Post: deanhystad
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,232 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  tabula-py, how to preserve a read_pdf() format and export to csv abcoelho 2 3,233 Mar-24-2021, 08:34 PM
Last Post: abcoelho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020