0

Using matplotlib.pyplot, I would like to make an affine transformation of a subplot.

Here is my use case: 1. I want to plot two subplots on the same row, 2. I want to add some text below the subplots (not using xticks or xlabels, but something like plt.text) 3. I need the text to be inside the figsize limit, i.e. the coordinates of the text must be between 0 and width/height.

My issue with my current code is that the subplots starts at 0 for the y-axis, which means that I print my text with negative coordinates for that axis. With the tool I'm using (Sphinx), it crops the plot and everything that is below 0 or above height is not displayed.

Here is a little example of what I'm doing right now. This doesn't work (some part of the text is not displayed).

import numpy as np
import matplotlib.pyplot as plt


plt.figure(figsize=(12, 4))

plt.subplot(121)
plt.plot(np.arange(10), 'o')
plt.xticks([], [])
for i in range(10):
    plt.text(i , - 1 + (i - 10) / 4, str(i), fontsize=17, color='C0')

plt.subplot(122)
plt.plot(np.arange(2, 12), 'o')
plt.xticks([], [])
for i in range(10):
    plt.text(i, 1 + (i - 10) / 4, str(i + 2), fontsize=17, color='C0')

plt.show()

I would like to limit the plots to a range of heights, for instance (1, 3), so that I could display the text with a positive coordinate. Obviously, if it can be done with another solution, I'd take it.

Do you have any idea how I could do that?

  • Check this link, if this gives you an idea. https://stackoverflow.com/questions/16872026/negative-axis-in-a-log-plot – Slayer Mar 26 '19 at 14:49
  • You can specify a transform to a text object: https://matplotlib.org/gallery/text_labels_and_annotations/text_alignment.html – Jody Klymak Mar 26 '19 at 15:05
  • 1
    Thanks for the replies! [matplotlib.pyplot.subplots_adjust](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots_adjust.html) did the trick. – johann.faouzi Mar 26 '19 at 15:12
  • @johann.faouzi I am glad, you found the answer. Please answer the question, so that it can help someone too. :D – Slayer Mar 26 '19 at 15:15

1 Answers1

0

matplotlib.pyplot.subplots_adjust did the trick: the whole text can be seen when the image is saved.

See this other post.