简体   繁体   中英

How to make an affine transformation of a plot?

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, ie 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?

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

See this other post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM