Hướng dẫn dùng interact matplotlib python
I upgraded from Python(x,y) 2.7.2.3 to 2.7.6.0 in Windows 7 (and was happy to see that I can finally type Show Previously (Spyder 2.1.9, IPython 0.10.2, matplotlib 1.2.1), when I plotted this script, for instance, it would plot the subplots side-by-side in an interactive window: Now (Spyder 2.2.5, IPython 1.2.0, Matplotlib 1.3.1) when I try to plot things, it does the subplots as tiny inline PNGs, which is a change in IPython: So I went into options and found this: which seems to say that I can get the old interactive plots back, with the 4 subplots displayed side-by-side, but when I switch to "Automatic", and try to plot something, it does nothing. No plots at all. If I switch this drop-down to Qt, or uncheck "Activate support", it only plots the first subplot, or part of it, and then stops: How do I get the old behavior of 4 side-by-side subplots in a single figure that I can interact with? To get interactive plots, we need to activate the figure. Using plt.ioff() and plt.ion(), we can perform interactive actions with plot. Steps
ExampleTo use interactive plot in Ipython - In [1]: %matplotlib auto Using matplotlib backend: GTK3Agg In [2]: import matplotlib.pyplot as plt In [3]: fig, ax = plt.subplots() # Diagram will get popped up, Let’s interact. In [4]: ln, = ax.plot(range(5)) # Drawing a line In [5]: ln.set_color('orange') # Changing drawn line to orange In [6]: plt.ioff() # Stopped interaction In [7]: ln.set_color('green') # Since we have stopped the interaction in the last step In [8]: plt.ion() # Started interaction OutputUpdated on 15-Mar-2021 08:31:31
Displaying Figures¶Plotly's Python graphing library, In general, there are five different approaches you can take in order to display
Each of the first three approaches is discussed below. Displaying Figures Using The renderers Framework¶The renderers framework is a flexible approach for displaying In [1]: import plotly.graph_objects as go fig = go.Figure( data=[go.Bar(y=[2, 1, 3])], layout_title_text="A Figure Displayed with fig.show()" ) fig.show() In most situations, you can omit the call to In [2]: import plotly.graph_objects as go fig = go.Figure( data=[go.Bar(y=[2, 1, 3])], layout_title_text="A Figure Displaying Itself" ) fig
In many contexts, an appropriate renderer will be chosen automatically and you will not need to perform any additional configuration. These contexts include the classic Jupyter Notebook, JupyterLab, Visual Studio Code notebooks, Google Colaboratory, Kaggle notebooks, Azure notebooks, and the Python interactive shell. Additional contexts are supported by choosing a compatible renderer including the IPython console, QtConsole, Spyder, and more. Next, we will show how to configure the default renderer. After that, we will describe all of the built-in renderers and discuss why you might choose to use each one.
Setting The Default Renderer¶The current and available renderers are configured using the In [3]: import plotly.io as pio pio.renderers Out[3]: Renderers configuration ----------------------- Default renderer: 'notebook_connected' Available renderers: ['plotly_mimetype', 'jupyterlab', 'nteract', 'vscode', 'notebook', 'notebook_connected', 'kaggle', 'azure', 'colab', 'cocalc', 'databricks', 'json', 'png', 'jpeg', 'jpg', 'svg', 'pdf', 'browser', 'firefox', 'chrome', 'chromium', 'iframe', 'iframe_connected', 'sphinx_gallery', 'sphinx_gallery_png'] The
default renderer that you see when you display
In [4]: import plotly.io as pio pio.renderers.default = "browser" It is also possible to set the default renderer using a system environment variable. At startup, Overriding The Default Renderer¶It is also possible to override the default renderer temporarily by passing the name of an available renderer as the In [5]: import plotly.graph_objects as go fig = go.Figure( data=[go.Bar(y=[2, 1, 3])], layout_title_text="A Figure Displayed with the 'svg' Renderer" ) fig.show(renderer="svg") −0.500.511.522.500.511.522.53A Figure Displayed with the 'svg' Renderer Built-in Renderers¶In this section, we will describe the built-in renderers so that you can choose the one(s) that best suit your needs. Interactive Renderers¶Interactive renderers display figures using the plotly.js JavaScript library and are fully interactive, supporting pan, zoom, hover tooltips, etc. notebook ¶This renderer is intended for use in the classic Jupyter Notebook (not JupyterLab). The full plotly.js JavaScript library bundle is added to the notebook the first time a figure is rendered, so this renderer will work without an Internet connection. This renderer is a good choice for notebooks that will be exported to HTML files (Either using nbconvert or the "Download as HTML" menu action) because the exported HTML files will work without an Internet connection.
notebook_connected ¶This renderer is the same as This renderer is a good choice for notebooks that will be shared with nbviewer since users must have an active Internet connection to access nbviewer in the first place. kaggle and azure ¶These are aliases for colab ¶This is a custom renderer for use with Google Colab. browser ¶ This renderer will open a figure in a browser tab using the default web browser. This renderer can only be used when the Python kernel is running locally on the same machine as the web browser, so it is not compatible with Jupyter Hub or online notebook services.
firefox ,
chrome , and chromium ¶These renderers are the same as the iframe and iframe_connected ¶These renderers write figures out as standalone HTML files and then display This renderer may be useful when working with notebooks than contain lots of large figures. When using the
The jupyterlab , nteract , and vscode ¶These are aliases for Static Image Renderers¶A set of renderers is provided for displaying figures as static images. These renderers all rely on the orca static image export utility. See the Static Image Export page for more information on getting set up with [orca]. png , jpeg , and svg ¶These renderers display figures as static In [6]: import plotly.graph_objects as go fig = go.Figure( data=[go.Bar(y=[2, 1, 3])], layout_title_text="A Figure Displayed with the 'png' Renderer" ) fig.show(renderer="png") PDF¶ This renderer displays figures as static PDF files. This is especially useful for notebooks that will be exported to PDF files using the LaTeX export capabilities of Other Miscellaneous Renderers¶JSON¶In editors that support it (JupyterLab, nteract, and the Visual Studio Code notebook interface), this renderer displays the JSON representation of a figure in a collapsible interactive tree structure. This can be very useful for examining the structure of complex figures. Multiple Renderers¶You can specify that multiple renderers should be used by joining their names on Customizing Built-In Renderers¶Most built-in renderers have configuration options to customize their behavior. To view a description of a renderer, including its configuration options, access the renderer object using dictionary-style key lookup on the In [7]: import plotly.io as pio png_renderer = pio.renderers["png"] png_renderer Out[7]: PngRenderer(width=None, height=None, scale=None, engine='auto') Renderer to display figures as static PNG images. This renderer requires either the kaleido package or the orca command-line utility and is broadly compatible across IPython environments (classic Jupyter Notebook, JupyterLab, QtConsole, VSCode, PyCharm, etc) and nbconvert targets (HTML, PDF, etc.). mime type: 'image/png' From this output, you can see that the Here is an example that customizes the In [8]: import plotly.io as pio png_renderer = pio.renderers["png"] png_renderer.width = 500 png_renderer.height = 500 pio.renderers.default = "png" import plotly.graph_objects as go fig = go.Figure( data=[go.Bar(y=[2, 1, 3])], layout_title_text="A Figure Displayed with the 'png' Renderer" ) fig.show() You can also override the
values of renderer parameters temporarily by passing them as keyword arguments to the In [9]: import plotly.graph_objects as go fig = go.Figure( data=[go.Bar(y=[2, 1, 3])], layout_title_text="A Figure Displayed with the 'png' Renderer" ) fig.show(renderer="png", width=800, height=300) Displaying figures in Dash¶Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run Get started with the official Dash docs and learn how to effortlessly style & deploy apps like this with Dash Enterprise. Plotly figures can be displayed in ipywidgets contexts using See the Plotly FigureWidget Overview for more information on integrating It is important to note that Performance¶No matter the approach chosen to display a figure, the figure data structure is first (automatically, internally) serialized into a JSON string before being transferred from the Python context to the browser (or to an HTML file first or to Kaleido for static image export). New in v5.0 The default JSON serialization mechanism can be slow for figures with many data points or with large Once a figure is serialized to JSON, it must be rendered by a browser, either immediately in the user's
browser, at some later point if the figure is exported to HTML, or immediately in Kaleido's internal headless browser for static image export. Rendering time is generally proportional to the total number of data points in the figure, the number of traces and the number of subplots. In situations where rendering performance is slow, we recommend considering the use of What About Dash?¶Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. Learn about how to install Dash at https://dash.plot.ly/installation. Everywhere in this page that you see import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) import dash import dash_core_components as dcc import dash_html_components as html app = dash.Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run_server(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter |