1

How to set to have the same scale/length of ticks in all subplots? I would like to set length of all xticks according to the forth subplot. I mean that all axis named y will have the same space between ticks 0 and 2, all axis named x will have the same space between -1 and 0. Maybe it would be sufficient to set plot as squares. How please?

import numpy as np
import matplotlib.pyplot as plt
from numpy import array
import matplotlib as mpl

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

# Plot figure with size
fig, ax = plt.subplots(sharex=True)
plt.figure(figsize=(12, 9))

# Subplots
fig1 = plt.subplot(231)
plt.plot(x, y**2)
fig1.set_xlim(0e-13,2e-13)
fig1.set_ylim(-1.15e-14,0.01e-14)

fig2=plt.subplot(232)
plt.plot(x, y**2)
fig2.set_xlim(0e-13,2e-13)
fig2.set_ylim(-7.3e-15,7.3e-15)

fig3=plt.subplot(233)
plt.plot(x, y**2)
fig3.set_ylim(0e-13,1.2e-13)
fig3.set_xlim(0e-13,2e-13)

# Subplots with arrows
fig4=plt.subplot(234)
plt.plot(x, y**2)
fig4.set_xlim(-1.15e-14,0.01e-14)
fig4.set_ylim(-7.3e-15,7.3e-15)

fig5=plt.subplot(235)
plt.plot(x, y**2)
fig5.set_xlim(-7.3e-15,7.3e-15)
fig5.set_ylim(0e-13,1.2e-13)
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)

fig6=plt.subplot(236)
plt.plot(x, y**2)
fig6.set_xlim(-1.5e-14,0e-14)
fig6.set_ylim(0e-13,1.2e-13)

plt.show()

enter image description here

Moe
  • 301
  • 2
  • 11

1 Answers1

1

The way to achieve this is outlined best by this excellent answer by @ImportanceOfBeingErnest. Basically you manually calculate the scaling to adhere to the ratio between the y and x limits of each existing axis by something like

fig1.set_aspect(np.diff(fig1.get_xlim())/np.diff(fig1.get_ylim()))

But please note that this must be done after any calls to set_ylim() and set_xlim() as it must use the final limits in order to correctly calculate the requisite ratio. set_xticks() and set_yticks() can safely be called before or after with the same effect.

Applying this to each of the six axes will produce

enter image description here

William Miller
  • 9,839
  • 3
  • 25
  • 46
  • Doesn’t that make this question a duplicate? – AMC Dec 08 '19 at 11:03
  • @AlexanderCécile To be quite honest I wasn't confident I knew what was being asked due to the wording of the question and title, therefore I'm not confident comparing it to the linked question at the level of certainty to raise a flag - particularly considering there are 6k waiting in the close vote queue. – William Miller Dec 08 '19 at 11:11
  • @AlexanderCécile Was at 8k a few days ago, and you need 3k rep to review those flags. 33's not bad compared to my 12... – William Miller Dec 08 '19 at 11:22
  • @WilliamMiller Wait, how do you get only 12? This site is just depressing, man... – AMC Dec 08 '19 at 11:23
  • @AlexanderCécile I've raised a whopping total of 17 flags haha, maybe because I spend on here most of my time answering – William Miller Dec 08 '19 at 11:27
  • 1
    @WilliamMiller Ah right, you get more flags based on the number of net helpful flags, not just reputation. Well you better start working on that close vote queue, I’m working overtime ;) – AMC Dec 08 '19 at 11:31
  • @AlexanderCécile At the rate you're going you might hit 3k rep before me, then it'll be your problem ;) – William Miller Dec 08 '19 at 11:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203840/discussion-between-william-miller-and-alexander-cecile). – William Miller Dec 08 '19 at 11:59