dexplo / dexplot

Simple plotting library that wraps Matplotlib and integrated with DataFrames

Home Page:https://dexplo.org/dexplot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to save the figures?

liuboss1992 opened this issue · comments

Dear sir, Great Job for dexplot!
Dexplot is a powerful tool, but how can i save a figure with dexplot? especially in a remote server without display environment?

It seems like those plot functions return a matplotlib Figure object so you can a Figure method to save plots.

import dexplot as dxp
import pandas as pd
import matplotlib.pyplot as plt

df = dxp.load_dataset('airbnb')

fig = dxp.scatter(x='bedrooms', y='price', data=df)
fig.savefig('plot.png')

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.savefig

Thanks for your works. How to display the figure in the jupyter? When I run it with jupyter notebook, it show me AttributeError: 'NoneType' object has no attribute 'print_figure'

@liuboss1992 All plotting functions return matplotlib figure objects which you can save with the savefig method as @StevenLi-DS showed.

@dehewujiang You should just be able to run it as the last line of code in any jupyter notebook cell and the image will be embedded in the notebook.

@tdpetrou Is there any reason why choose Figure over Axes?

Yes - a few reasons. The figure is the entire 'plot' and contains the axes. When there are multiple axes (when using row or col, then returning axes wouldn't make sense. Returning the figure is consistent.

Also, the figures are never embedded into the notebook unless they are the last line of code. Seaborn invokes pyplot to create the figures which automatically embeds the figure in the notebook and it also returns the axes which puts an ugly statement in your notebook (<matplotlib.axes._subplots.AxesSubplot at 0x119ced5b0>). Returning the figure is far superior in my opinion.

@tdpetrou I do agree that the inconsistent issue is indeed a problem in seaborn. But I often need to combine many axes into one figure, even different axes returned by different packages. Meanwhile, Axes is also way flexible than Figure in terms of the number of methods available. I wonder if you have any plan to address this problem (maybe add an arg return_axes=True) or this package is just designed to be used with itself.

I don't really mind the "ugly statement" and sometimes if you have annotation text in matplotlib, it will return some text output anyway. I just add a ";" to hide them.

@StevenLi-DS I think it would be possible to return the Axes, like you said, using an extra parameter. But, I would have to change the way the figure is created so that it gets embedded in the notebook so that the only thing you are returned is not

<matplotlib.axes._subplots.AxesSubplot at 0x11c5513d0>

This isn't difficult by any means (I'll just use plt.figure instead of plt.Figure). I think its a good idea, so I'll probably implement it.

For now, you can use dxp.plotting_func(...).axes[0]

@tdpetrou Thanks. If possible, I really think the returned data structure should be Axes by default for better compatibility and returning Figure only if the plot function generates a matrix or something.

For the workaround, I tried the following but it doesn't seem to work. Am I missing something?

import dexplot as dxp
import pandas as pd
import matplotlib.pyplot as plt

df = dxp.load_dataset('airbnb')

fig, ax = plt.subplots(figsize=(5, 5))
ax = dxp.scatter(x='bedrooms', y='price', data=df).axes[0]