Convert a matplotlib figure to plotly dictionary and send. All available information about matplotlib visualizations are stored within a matplotlib.figure.Figure object. You can create a plot in python using matplotlib, store the figure object, and then pass this object to the fig_t
(fig, resize=False, strip_style=False, verbose=False)
| 57 | |
| 58 | ### mpl-related tools ### |
| 59 | def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False): |
| 60 | """Convert a matplotlib figure to plotly dictionary and send. |
| 61 | |
| 62 | All available information about matplotlib visualizations are stored |
| 63 | within a matplotlib.figure.Figure object. You can create a plot in python |
| 64 | using matplotlib, store the figure object, and then pass this object to |
| 65 | the fig_to_plotly function. In the background, mplexporter is used to |
| 66 | crawl through the mpl figure object for appropriate information. This |
| 67 | information is then systematically sent to the PlotlyRenderer which |
| 68 | creates the JSON structure used to make plotly visualizations. Finally, |
| 69 | these dictionaries are sent to plotly and your browser should open up a |
| 70 | new tab for viewing! Optionally, if you're working in IPython, you can |
| 71 | set notebook=True and the PlotlyRenderer will call plotly.iplot instead |
| 72 | of plotly.plot to have the graph appear directly in the IPython notebook. |
| 73 | |
| 74 | Note, this function gives the user access to a simple, one-line way to |
| 75 | render an mpl figure in plotly. If you need to trouble shoot, you can do |
| 76 | this step manually by NOT running this fuction and entereing the following: |
| 77 | |
| 78 | =========================================================================== |
| 79 | from plotly.matplotlylib import mplexporter, PlotlyRenderer |
| 80 | |
| 81 | # create an mpl figure and store it under a varialble 'fig' |
| 82 | |
| 83 | renderer = PlotlyRenderer() |
| 84 | exporter = mplexporter.Exporter(renderer) |
| 85 | exporter.run(fig) |
| 86 | =========================================================================== |
| 87 | |
| 88 | You can then inspect the JSON structures by accessing these: |
| 89 | |
| 90 | renderer.layout -- a plotly layout dictionary |
| 91 | renderer.data -- a list of plotly data dictionaries |
| 92 | """ |
| 93 | matplotlylib = optional_imports.get_module("plotly.matplotlylib") |
| 94 | if matplotlylib: |
| 95 | renderer = matplotlylib.PlotlyRenderer() |
| 96 | matplotlylib.Exporter(renderer).run(fig) |
| 97 | if resize: |
| 98 | renderer.resize() |
| 99 | if strip_style: |
| 100 | renderer.strip_style() |
| 101 | if verbose: |
| 102 | print(renderer.msg) |
| 103 | return renderer.plotly_fig |
| 104 | else: |
| 105 | warnings.warn( |
| 106 | "To use Plotly's matplotlylib functionality, you'll need to have " |
| 107 | "matplotlib successfully installed with all of its dependencies. " |
| 108 | "You're getting this error because matplotlib or one of its " |
| 109 | "dependencies doesn't seem to be installed correctly." |
| 110 | ) |
| 111 | |
| 112 | |
| 113 | ### graph_objs related tools ### |
nothing calls this directly
no test coverage detected