1

I managed to add text on the figure with

 fg_ax.text( 0.05, 0.1, f"n = {len(df)}", ) 

fg_ax being my matplotlib.axes object

But I want it below the figure. Because my y coordinates go from 0 to 1, I figured after reading the documentation that doing something like this would put it below:

 fg_ax.text( 0.05, -0.5, f"n = {len(df)}", ) 

But there is nothing that appears anymore, as if I was writing "outside" what is displayed.

I tried plt.show() and fg_ax.figure.savefig. None works.

Minimal reproducible example:

import numpy as np import matplotlib.pyplot as plt N = 50 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.rand(N) area = (30 * np.random.rand(N)) ** 2 # 0 to 15 point radii _, fg_ax = plt.subplots() fg_ax.scatter(x, y, s=area, c=colors, alpha=0.5) fg_ax.text( 0.05, -0.5, f"n = qsdfqsdf", ) plt.show() 
5
  • When I run the code in your MRE with no modifications a figure is generated that shows the text "n = qsdfqsdf" below the axes/plot area. Maybe something is cropping your figure display area? Commented Dec 11, 2022 at 22:09
  • This is what I get: i.imgur.com/5cFHr9v.png Commented Dec 11, 2022 at 22:54
  • I'm using PyCharm, maybe you are using another IDE? Commented Dec 11, 2022 at 22:55
  • I'm using the IDE Spyder 5.3.1. The plot is rendering in-line in a pane of the IDE window: imgur.com/a/EIp8crv Commented Dec 12, 2022 at 6:13
  • That's exactly what I would want @DoodleVib. Do you know how to save this on a .pdf or a .png so that this works (with fg_ax.figure.savefig)? Commented Dec 12, 2022 at 12:50

2 Answers 2

3

Based on this answer, I've changed your code to the following:

N = 50 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.rand(N) area = (30 * np.random.rand(N)) ** 2 # 0 to 15 point radii fig, fg_ax = plt.subplots() fg_ax.scatter(x, y, s=area, c=colors, alpha=0.5) plt.figtext( 0.2, 0.01, f"n = qsdfqsdf", wrap=True, horizontalalignment='center', fontsize=10 ) fig.subplots_adjust(bottom=0.3) plt.savefig("image.pdf", format="pdf", bbox_inches="tight") plt.show() 

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Although it works, I cannot make the text go "more down" than y = 0.0.
I've edited my answer: is this better? Now, you can also save the image as a pdf.
verticalalignment='top' will let text overflow below y=0
1

Here's an example of using verticalalignment='top' to put text below the existing figure, effectively extending the figure downwards:

import matplotlib.pyplot as plt text = "{\n Metadata,\n Unknown,\n Amount,\n}" fig, axs = plt.subplots(2,2,figsize=(3,3)) fig.text(0, 0, text, va='top') fig.tight_layout() fig.savefig('test.png', bbox_inches="tight") 

Which gives:

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.