4

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
  • 10,251
  • 6
  • 69
  • 98

2 Answers2

4

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
  • 289,005
  • 45
  • 571
  • 615
-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
  • 8,807
  • 8
  • 50
  • 99
Sraw
  • 17,016
  • 6
  • 45
  • 76
  • 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