Search code examples
matplotlibmetpy

suppress printing axis label in metpy


I am generating SkewT plots using MetPy. I am trying to suppress the automatic generation of axis labels and use my own. The automatic labels generated are hectopascal and degree_Celsius.I can't find a way to disable the automatic axis labels so the end result is a messy looking plot. The unwanted auto-generated labels are circled in muddy red:

Plot with unwanted extra labels

I tried turning them off by adding

plt.axis('off')

but not only did it remove the labels it removed the tick marks and everything, even the border around the plot.

I tried getting more creative with

plt.gca().axes.yaxis.set_label([])

or

skew.ax.xaxis.set_label([])

but neither of these worked. Other things I tried for removing the labels also removed the tick values.

I know I'm probably missing something simple but I can't seem to locate it.

Here's a sample code that demonstrates this using a slightly modified SkewT plot example from MetPy:

import matplotlib.pyplot as plt
import numpy as np
import metpy.calc as mpcalc
from metpy.plots import SkewT
from metpy.units import units

fig = plt.figure(figsize=(9, 9))
skew = SkewT(fig, rotation=45)

# Create arrays of pressure, temperature, dewpoint, and wind components
p = [902, 897, 893, 889, 883, 874, 866, 857, 849, 841, 833, 824, 812, 796, 776, 751,
     727, 704, 680, 656, 629, 597, 565, 533, 501, 468, 435, 401, 366, 331, 295, 258,
     220, 182, 144, 106] * units.hPa
t = [-3, -3.7, -4.1, -4.5, -5.1, -5.8, -6.5, -7.2, -7.9, -8.6, -8.9, -7.6, -6, -5.1,
     -5.2, -5.6, -5.4, -4.9, -5.2, -6.3, -8.4, -11.5, -14.9, -18.4, -21.9, -25.4,
     -28, -32, -37, -43, -49, -54, -56, -57, -58, -60] * units.degC
td = [-22, -22.1, -22.2, -22.3, -22.4, -22.5, -22.6, -22.7, -22.8, -22.9, -22.4,
      -21.6, -21.6, -21.9, -23.6, -27.1, -31, -38, -44, -46, -43, -37, -34, -36,
      -42, -46, -49, -48, -47, -49, -55, -63, -72, -88, -93, -92] * units.degC

# Calculate parcel profile
prof = mpcalc.parcel_profile(p, t[0], td[0]).to('degC')
u = np.linspace(-10, 10, len(p)) * units.knots
v = np.linspace(-20, 20, len(p)) * units.knots

skew.plot(p, t, 'r', linewidth=4)
skew.plot(p, td, 'g', linewidth=4)
skew.plot(p, prof, 'k', linewidth=3)  # Plot parcel profile
skew.plot_barbs(p[::5], u[::5], v[::5])

skew.ax.set_xlim(-50, 30)
skew.ax.set_ylim(1000, 150)
skew.ax.xaxis.set_tick_params(which='major', labelsize=15, direction='out')
skew.ax.yaxis.set_tick_params(which='major', labelsize=15, direction='out')

#THIS DIDN'T WORK
#skew.ax.xaxis.set_label([])  

# Add the relevant special lines
skew.plot_dry_adiabats()
skew.plot_moist_adiabats()
skew.plot_mixing_lines()

# Labels
fig.text(0.52,0.05, "temperature ($^{o}$C)", ha="center", va="center", fontsize=18)
fig.text(0.04,0.5, "pressure (hPa)", ha="center", va="center", rotation=90, fontsize=18)

plt.show()

Thanks


Solution

  • So the functions you're looking for are set_xlabel or set_ylabel:

    # Clear out the default labels
    skew.ax.set_xlabel('')
    skew.ax.set_ylabel('')
    

    If you really want to use the methods on .xaxis and .yaxis, it would be:

    skew.ax.xaxis.set_label_text('')
    skew.ax.yaxis.set_label_text('')
    

    I wanted to also point out that you don't just have to clear out the labels, but you should be able to use those methods to achieve what you're doing with fig.text:

    # Use \N for named unicode character
    skew.ax.set_xlabel('temperature (\N{DEGREE CELSIUS})',
                       fontdict=dict(size=18))
    skew.ax.set_ylabel('pressure (hPa)', fontdict=dict(size=18))