1

Given

import matplotlib.pyplot as plt
import random 

fig, ax = plt.subplots(1, 2, figsize = [9, 5], dpi = 100) 
xs = [random.randint(-10, 10) for _ in range(10)]
ys = [random.randint(-10, 10) for _ in range(10)]

xx = [random.randint(-20, 20) for _ in range(10)]
yy = [random.randint(-20, 20) for _ in range(10)]

ax[0].plot(xs, ys)
ax[1].plot(xx, yy)

plt.show()

which produces

enter image description here

I want the xticks and yticks of the second plot to be the same as that of the first plot without affecting the plot's data that is displayed. So basically I want to just transform / mask the ticks to match that of the first plot and keep every thing else intact.

Here is an example of what I am trying to achieve. Notice the ticks enter image description here

I have tried

  • ax[1].xaxis.set_major_formatter(ax[0].xaxis.get_major_formatter())
  • ax[1].set_xticks(ax[0].get_xticks())
  • ax[1].set_xticklabels(ax[0].get_xticklabels())
  • Some other ticks.Formatter stuff that transforms each element individually but that seems like overkill

none of these do what I want to achieve.


Edit for clarification

People don't seem to understand what I am asking for. I do not want the plot to be affected (zoomed-in, zoomed-out, translated, rotated) when changing the tick labels. So given ax[1] and ax[2] the exact same plot, something like

xmin,xmax = ax[0].get_xlim()
ymin,ymax = ax[0].get_ylim()
ax[2].set_xlim(xmin,xmax)
ax[2].set_ylim(ymin,ymax)

produces

enter image description here

notice how the third plot (with changed tick labels) is zommed-in compared to the second, which is not what I want. I want the second plot and third plot to remain the same but to have different ticks (to match that of the first plot)

hexaquark
  • 883
  • 4
  • 16
  • Does this answer your question? [How to set equal number of ticks for two subplots?](https://stackoverflow.com/questions/59105437/how-to-set-equal-number-of-ticks-for-two-subplots) – Jody Klymak Oct 13 '22 at 23:56
  • @JodyKlymak No, I added a clarification edit. – hexaquark Oct 14 '22 at 17:07

1 Answers1

0

Try this

xmin,xmax = ax[0].get_xlim()
ymin,ymax = ax[0].get_ylim()
ax[1].set_xlim(xmin,xmax)
ax[1].set_ylim(ymin,ymax)

enter image description here

Li Yupeng
  • 721
  • 4
  • 9
  • No. That just zooms-in the plot. This is not what I am looking for. I specified that I want the data and the plot to not change and remain identical. I just want to change the tick labels with a mask. – hexaquark Oct 14 '22 at 16:58