5

My first python question, though.

Consider me working in jupyter notebook with seaborn. Suppose I've created a plot and saved it to a variable:

g = sns.jointplot(ap['ap_lo'], ap['ap_hi']);

It will be displayed once, because I addeed %matplotlib inline at the beginning.

Now, after few changes, I want to display g again:

ax = g.ax_joint
ax.set_xscale('log')
ax.set_yscale('log')
g.ax_marg_x.set_xscale('log')
g.ax_marg_y.set_yscale('log')

sns.plt.show()

As you can see, I call sns.plt.show(), but it has no effect. I've also tried to put g; or g; sns.plt.show() at the end.

kelin
  • 11,323
  • 6
  • 67
  • 104

2 Answers2

5

In the case of the question, g is a JointGrid instance.

In order to show a figure in a jupyter notebook cell, you need to state the figure. plt.show will not work in a new cell. The figure created by a JointGrid is available via the fig attribute. Hence the solution is to type

g.fig

Complete example image:

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Looks like `.fig` is deprecated (https://seaborn.pydata.org/generated/seaborn.JointGrid.html) it prefers `.figure`. Further, `.figure` has a `.show()` method (https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure) so you can run `g = sns.jointplot(..)` then `g.figure.show()` – travelingbones Mar 14 '23 at 21:23
-1

In seaborn's documentation, sns.jointplot() returns a JointGrid object. And this object has some functions to display plot.

You can check documentation here.

In practice, you need to call g.plot or g.plot_joint to display your plot.

Updated

After diving into source code, I find joinplot use the following functions if you use default parameters:

joint_kws = {}
joint_kws.update(kwargs)

marginal_kws = {}

color = color_palette()[0]
color_rgb = mpl.colors.colorConverter.to_rgb(color)
colors = [utils.set_hls_values(color_rgb, l=l)
          for l in np.linspace(1, 0, 12)]

grid = JointGrid(x, y, data, dropna=dropna,
                 size=size, ratio=ratio, space=space,
                 xlim=xlim, ylim=ylim)
...
joint_kws.setdefault("color", color)
grid.plot_joint(plt.scatter, **joint_kws)

marginal_kws.setdefault("kde", False)
marginal_kws.setdefault("color", color)
grid.plot_marginals(distplot, **marginal_kws)
...
zell
  • 9,830
  • 10
  • 62
  • 115
Sraw
  • 18,892
  • 11
  • 54
  • 87
  • Thanks for the answer. Both `plot` and `plot_joint` require parameters. What parameters should I pass to this functions if I wan't to leave the plot unchanged? Only the scale should become logarithmic. – kelin Sep 19 '17 at 07:02
  • I also looked at the source code and red the documentation more carefully. `plot` is equivalent to creating a new plot on the JointGreed, and `plot_joint` creates the dots. I need to display an existing plot with modified axes scale, so I can't accept this answer. – kelin Sep 19 '17 at 08:36