Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I can modify a plot = sympy.plot(..., show=False) object modifying the attributes of plot and later invoking plot.show() .

If I need to customize some aspects of the plot not directly exposed by plot , e.g., the size of the axes'labels, I can access the Matplotlib backend of my plot, be = plot._backend with the condition that I had already showed (as in plot.show() ) my plot.

This work nicely in a terminal IPython session (using the %matplotlib magic) because the figure is constantly updated and works (not so nicely) in a script, because for various reasons all I can do, but tipically is good enough, is to use the savefig method of the backend, be.fig.savefig(...) .

Enter the Jupiter notebook. For performance reasons I prefer to use the magic %matplotlib inline , so if I want to access the Matplotlib backend I have to instantiate the plot in the output cell, but later any modification I put in place is lost because the plot is no more updated... again, all I can do is a savefig . If I try to do

be.fig.show()

I receive an error message

/home/boffi/lib/miniconda3/lib/python3.6/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, "

Is it possible to do what I'd like to do, i.e., modifying the details of a Sympy's plot using its Matplotlib backend inside of a Jupyter notebook and using the %matplotlib inline magic?

I corrected the misspelling --- but sympy is in the tags, isn't it?. What I want to modify in the plot is something that, not being exposed by the plot obiect, I have to access using the plot backend. The size of the axis labels is the itch that I'm currently scratching but i feel that here the problem is more general — I may be wrong… – gboffi Mar 16, 2018 at 12:13 import sympy imports a good deal of names from Sympy sub-modules, sympy.plot is sympy.plotting.plot value is True, yes I mean sympy.plotting.plot. sympy.plotting.plot(...) returns an object of type sympy.plotting.Plot that, in the body of my question, is referred as plot. – gboffi Mar 16, 2018 at 12:22

A sympy plot is a matplotlib plot. Now it surely depends on what you want to do. For most stilistic adaptions you may just set the respective rcParams before plotting.

%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams["xtick.labelsize"] = 16
plt.rcParams["xtick.color"] = "red"
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')
p = plot(x**2, (x, -5, 5))

In addition it should indeed be possible to modify the figure and axes after creating the figure.

fig = p._backend.fig
ax = fig.axes[0]
ax.set_xticks([-4,4])
for i, label in enumerate(ax.get_yticklabels()):
    label.set_rotation(i*15)
    label.set_size(15)
    label.set_color((1.-i/10.,0,i/10.))
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.