Path: blob/main/examples/reference/panes/Matplotlib.ipynb
3206 views
The Matplotlib pane allows displaying Matplotlib figures inside a Panel app. This includes figures created by Seaborn, Pandas .plot, Plotnine and any other plotting library building on top of Matplotlib.
The Matplotlib pane will render the object to PNG or SVG at the declared DPI and then display it.
Parameters:
alt_text(str, default=None): alt text to add to the image tag. The alt text is shown when a user cannot load or display the image.dpi(int, default=144): The dots per inch of the exported png.encode(bool, default=False): Whether to encode 'svg' as base64. Default is False. 'png' will always be encoded.fixed_aspect(boolean, default=True): Whether the aspect ratio of the figure should be forced to be equal.format(str, default='png'): The format to render the figure to: 'png' or 'svg'.high_dpi(bool, default=True): Whether to optimize output for high-dpi displays.interactive(boolean, default=False): Whether to use the interactive ipympl backend.link_url(str, default=None): A link URL to make the figure clickable and link to some other website.object(matplotlib.Figure): The MatplotlibFigureobject to display.tight(bool, default=False): Automatically adjust the figure size to fit the subplots and other artist elements.
Resources
By modifying the figure and using the trigger method on the pane's object we can easily update the plot:
Alternatively, like all other models, a Matplotlib pane can be updated by setting the object directly:
Using the Matplotlib pyplot interface
You might have noticed that we did not use the matplotlib.pyplot API above. We did this in order to avoid having to specifically close the figure. If the figure is not closed, it will cause memory leaks.
You can use the matplotlib.pyplot interface, but then you must specifically close the figure as shown below!
Fixing clipping issues with tight=True
If you find the figure to be clipped on the edges you can set tight=True.
Responsive plots
If you want to make your plots responsively fit what ever container they are inside, then you should be using the appropriate sizing_mode in combination with
format="svg": to get better looking resized plots,fixed_aspect=True: to allow the 'svg' image to resize its height and width independently and/ orfixed_aspect=False(default): to allow the 'svg' image to resize its height and width while keeping the aspect ratio.
Lets start by displaying using the default 'png' format and sizing_mode="stretch_width".
If you have a wide window you will see some large, pink areas on the sides. If you decrease the window width, then you will see the plot responsively resize.
Using the 'svg' format you can make the figure take up the full width.
But that might make the figure too high. Lets try with a fixed height
But maybe we want the figure to take up the full width. Lets change the fixed_aspect to False.
In summary you should be able to achieve the kind of responsive sizing you need by using the appropriate combination of format, fixed_aspect and sizing_mode values.
Using the interactive Matplotlib backend
If you have installed ipympl you will also be able to use the interactive backend:
Using Seaborn
We recommend creating a Matplotlib Figure and providing it to Seaborn
You can also use Seaborn directly, but then you must remember to close the the Figure manually to avoid memory leaks.
You can remove the Seaborn theme via matplotlib.rcdefaults()
Using Pandas .plot
We recommend creating a Matplotlib Figure and providing it to pandas.plot.
Using Plotnine
The plotnine.ggplot.draw method will return the Matplotlib Figure object.
Please note you must close the figure your self.
Controls
The Matplotlib pane exposes a number of options which can be changed from both Python and Javascript. Try out the effect of these parameters interactively: