Home > Back-end >  Ticks and Labels on Twin Axes
Ticks and Labels on Twin Axes

Time:09-11

How can I set the labels on the extra axes?

The ticks and labels should be the same on all 4 axes. I'm doing something wrong... Thanks!

import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True

plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twiny()
plt.show()

# create reusable ticks and labels
ticks = [0,1/2,3.14159/4,3.14159/2,1]
labels = [r"$0$", r"$\displaystyle\frac{1}{2}$", r"$\displaystyle\frac{\pi}{4}$", r"$\displaystyle\frac{\pi}{2}$", r"$1$"]

# Version 1: twinx()   xaxis.set_ticks()
plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twiny()
ax1.xaxis.set_ticks(ticks, labels=labels)
ax1.yaxis.set_ticks(ticks, labels=labels)
ax2.xaxis.set_ticks(ticks, labels=labels)
ax3.yaxis.set_ticks(ticks, labels=labels)
plt.show()

# Version 2: twinx()   set_xticklabels)()
plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twiny()
ax1.set_xticks(ticks)
ax1.set_xticklabels(labels)
ax1.set_yticks(ticks)
ax1.set_yticklabels(labels)
ax2.set_xticks(ticks)
ax2.set_xticklabels(labels)
ax3.set_yticks(ticks)
ax3.set_yticklabels(labels)
plt.show()

enter image description here

Confused: How come ax1 has both xaxis and yaxis, while ax2, ax3 do not appear to?

CodePudding user response:

A unintuitive solution based on enter image description here

Code:

import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True


# create reusable ticks and labels
ticks = [0,1/2,3.14159/4,3.14159/2,1]
labels = [r"$0$", r"$\displaystyle\frac{1}{2}$", r"$\displaystyle\frac{\pi}{4}$", r"$\displaystyle\frac{\pi}{2}$", r"$1$"]



plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()

ax1.xaxis.set_ticks(ticks, labels=labels)
ax1.yaxis.set_ticks(ticks, labels=labels)


ax2_x = ax1.twiny()  # switch
ax3_y = ax1.twinx()  # switch

ax2_x.xaxis.set_ticks(ticks, labels=labels) 
ax3_y.yaxis.set_ticks(ticks, labels=labels)

plt.show()

Or switch the x/yaxis.set_ticks - with the same effect:

  • On second thought, I assume that's the preferred way to do it, especially when data comes into play.
ax2_x = ax1.twinx()
ax3_y = ax1.twiny()
    
ax2_x.yaxis.set_ticks(ticks, labels=labels)  # switch
ax3_y.xaxis.set_ticks(ticks, labels=labels)  # switch

In case you don't intend to use the twin axis functionality (that means having different data with different scaling assigned to those axis) but 'only' want the ticks and labels on all 4 axis for better plot readability:

Solution based on answer of ImportanceOfBeingErnest with the same plot result:

import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True


# create reusable ticks and labels
ticks = [0,1/2,3.14159/4,3.14159/2,1]
labels = [r"$0$", r"$\displaystyle\frac{1}{2}$", r"$\displaystyle\frac{\pi}{4}$", r"$\displaystyle\frac{\pi}{2}$", r"$1$"]


plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()

ax1.xaxis.set_ticks(ticks, labels=labels)
ax1.yaxis.set_ticks(ticks, labels=labels)
ax1.tick_params(axis="x", bottom=True, top=True, labelbottom=True, labeltop=True)
ax1.tick_params(axis="y", left=True, right=True, labelleft=True, labelright=True)

plt.show()
  • Related