2

If I have some plot nested in some loop, lets say:

import matplotlib.pyplot.plot as plot
import matplotlib.pyplot at plt

x = [1, 2, 3, 4]
plt.figure()
plot(x)
plt.title('some title with the {name}')

The title is dynamic, and I often want to copy the name into my clipboard after viewing the plot. Is there a way to make the title of my plot copy-able?

Another note, I am using Spyder 4.0.1, and my plots appear in its plot pane.

Travasaurus
  • 601
  • 1
  • 8
  • 26

1 Answers1

5

An image (such as a matplotlib canvas) is an array of intensity values that we call pixels. It does not inherently contain any textual information, even if your brain interprets some of the arrangements of light patterns as text. You should therefore make the string copyable as a string, outside your plot.

There is a simple way, but you have to think outside the plot:

import matplotlib.pyplot.plot as plot
import matplotlib.pyplot at plt

x = [1, 2, 3, 4]
plt.figure()
plot(x)

t = 'some title with the {name}'
plt.title(t)
print(t)

Now you can freely copy the textual representation of the title off of the command line.

If you are OK with additional dependencies, you can copy directly to the clipboard, as recommended in this question. For example, with pyperclip installed, you can do:

import pyperclip

pyperclip.copy(t)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • That sounds reasonable. I guess I just didn't understand that pngs are just one solid item (noob I know). Thanks for the response! – Travasaurus Feb 04 '20 at 21:42
  • 1
    @Travasaurus. I've updated with another option. Feel free to select if it works for you. – Mad Physicist Feb 04 '20 at 21:44
  • Is there a way to display inline plots as PDFs? or is that not possible? – Travasaurus Feb 04 '20 at 21:45
  • 1
    @Travasaurus. A PDF would be a container that would allow you to store the image. I don't think that you can display inline plots as anything other than an image inline. – Mad Physicist Feb 04 '20 at 21:48