-1

I've run Seaborn's clustermap and saved the result into a ClusterGrid object named "test". However due to some unknown reason that command didn't show the clustermap unless I run it again, although the "test" isn't empty. It is indeed a ClusterGrid object.

So is there any simple way to show/plot the ClusterGrid since I've saved it? I tried plt.plot but it didn't work.

Code42
  • 2,292
  • 1
  • 17
  • 22
  • Display to the output area can sometimes be buggy with plots and re-running it is just easier. But you should be able to make your other approaches work. Since you assigned it to `test`, did you try calling `test.figure` in a cell below your code? Seaborn uses matplotlib under the hood and so generally approaches to save a matplotlib plot as an image work. I think it would be `test.savefig()` but on mobile device right now, and so cannot look. After saving it as an image, I prefer to use a code cell to call it so image gets encoded as base64 in the notebook. That way it shows in static view. – Wayne Jan 25 '20 at 19:21
  • The seventh code cell in this [notebook](https://github.com/fomightez/cl_sq_demo-binder/blob/master/notebooks/Demo%20of%20script%20to%20calculate%20differences%20between%20sequences%20in%20multiFASTA%20file.ipynb) illustrates using `Image()` to display an image in output that will save it as base64 inside notebook code. If you just display it with markdown, you’d need to keep another file along with the notebook for later viewing. – Wayne Jan 25 '20 at 19:29
  • Looks like to save image as a file, you may need `test.figure.savefig()`, see [this line of code](https://github.com/fomightez/sequencework/blob/4b07bb99ed15465ca8bec898d3d2b864eff38c61/alignment-utilities/score_differences_between_sequences_by_pairwise_alignment.py#L440) and code just above as a guide. The plot was a Seaborn generated heat map. Some plots have different attributes and methods so don’t be completely shocked if have to try more variations or look for a specific plot example to get the exact syntax here. – Wayne Jan 25 '20 at 19:38
  • Also, I meant to ask first, did you try putting `%matplotlib inline` or `%matplotlib notebook` as first line of a cell above your plot cell? – Wayne Jan 25 '20 at 19:44
  • @wayne test.figure gives me an error: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in ----> 1 test.figure AttributeError: 'ClusterGrid' object has no attribute 'figure' And indeed I have %matplotlib inline – Code42 Jan 26 '20 at 23:34

1 Answers1

1

Typing dir(test) should show you methods and attributes of the seaborn.matrix.ClusterGrid. (seaborn.matrix.ClusterGrid is what you should see if you type type(test).) Among the list are two items, fig and savefig.

test.fig will allow you to display the plot again any cell below the one that generated it, assuming %matplotlib inline was invoked earlier. So if it is displaying unreliably in the generating cell, put a cell with test.fig right below that one and you'll see it.

test.savefig("test_plot.png") should save the plot as an image file. You can use code to show that image with from IPython.display import Image; Image("test_plot_.png"), and that will get embedded in the saved notebook and shown subsequently in static renders.

Wayne
  • 6,607
  • 8
  • 36
  • 93