In the following example I use Sympy to make a plot:
from sympy import symbols, plot
x = symbols('x')
p = plot(x*x, x**3, (x, -2, 2), show=False)
for n, line in enumerate(p, 2):
line.label='$x^{%d}$'%n
line.line_color = ['k', 'r'][n-2]
p.legend = True
As you can see, the legend is placed over the lines and Sympy doesn't offer a direct way to change the position of the legend.
After some research I found, directly in the source code of
*/sympy/plotting/plot.py
, this comment:
Especially if you need publication ready graphs and this module is not enough for you - just get the
_backend
attribute and add whatever you want directly to it. In the case of matplotlib (the common way to graph data in python) just copy_backend.fig
which is the figure and_backend.ax
which is the axis and work on them as you would on any other matplotlib object.
Hence I tried
be = p._backend
but what I've got back is an:
AttributeError: 'Plot' object has no attribute '_backend'
What should I do to move the legend or to otherwise tweak the plot
using this ._backend
attribute?
▶ U P D A T E ◀
After another trip to the source code I realized that the ._backend
attribute is instantiated only after the plot is committed to the screen,
as in p.show()
.
With this new knowledge, always in the interactive interpreter, I tried
...
p.show()
p._backend.ax.legend(loc='4') # bottom right
and the plot was updated with the legend location in the "correct" place.
Have I solved my problem? I'm afraid I've not, because this works in
an IPython session, when you have issued the IPython's magic
%matplotlib
(that enables to interact with a live plot) and, afaict,
only under these conditions.
In particular. the following code, executed as a script,
from sympy import symbols, plot
x = symbols('x')
p = plot(x*x, x**3, (x, -2, 2), show=False)
for n, line in enumerate(p, 2):
line.label='$x^{%d}$'%n
line.line_color = ['k', 'r'][n-2]
p.legend = True
p.show()
p._backend.ax.legend(loc=4) # bottom-right
p.save('plot_with_legend_OK_maybe.png')
saves the plot with the legend in the top-right corner, over the plotted lines.
So here it is the updated version of my
Q U E S T I O N
Is it possible to change the plot, using its .backend
attribute, and have the changes persisted in a saved image file?