Plotting and the show() method

asked 2012-12-08 18:54:30 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hi Wisdom wranglers,

I'm a bit puzzled by the plot2d interface in sage. Using the cli interface, I discovered that I had to use the list_plot().show() method to get the png to show. Here is a slice of the array passed to list_plot():

[(0.000000000000000, -22.605560302734375), (0.108611997882434, -22.714309692382812), (0.219268673499272, -22.81563949584961), (0.331878474116081, -22.90947914123535)]

Without the show() there was no error, but no plot. I'm puzzled because the examples in the plot2d documentation do not indicate that show() is necessary.

Question: when is the show() method necessary for list_plot() or other plot functions?

Note: I am using the cli rather than notebook interface. Perhaps this is the reason. (I simply don't have the bandwidth (or the inclination) to run a notebook server on my tiny computer)

edit retag flag offensive close merge delete


Couple of questions: 1. What is the exact command you are using, and what version of Sage are you using? 2. What output does the command `show_default()` return? Just using something like `sage: list_plot(...)` will work by itself. If you assign your plot to some variable, like `sage: p = list_plot(...)`, it won't work. The actual rendering of the plot happens only when the plot is about to be "printed", that is when the `_repr_` method is called. The command `show_default()` controls what is output when you use the former command. If it is set to `False` then you will still get a text output like `Graphics object consisting of 1 graphics primitive` - do you get this?

ppurka gravatar imageppurka ( 2012-12-08 23:12:06 +0200 )edit

Hi ppurka, 1. The exact command is as follows: list_plot(self.ellipse,axes='true',aspect_ratio='automatic').show() where self.ellipse slice as follows: [[-0.2502810527046193, -0.9528485681543885], [-0.23332252056744662, -0.9570135783421186], [-0.2162905291096238, -0.9608821121646137], [-0.19918932688797977, -0.9644526012140253]...etc] (Note: This is the snippet of source code which requires the show() command.) 1a. I just tested the same source code in the notebook, and the show() is also needed. Omitting the show() and no-show of the ellipse (: 2. show_default() is True When set to False and show() not called, there is no text output of the Graphic i.e. same behaviour as when set to True - need show() 3. My version of sage: Sage Version 5.0.1, Release Date: 2012-06-10 tx!

PatB gravatar imagePatB ( 2012-12-08 23:19:33 +0200 )edit

Can you get something like `list_plot([[1,2],[2,3],[3,4]])` to work? I'm running the same version of Sage on an older MacBook in the command line and list_plot will launch a window with the plot in it.

calc314 gravatar imagecalc314 ( 2012-12-09 00:30:25 +0200 )edit

I can induce the situation you are getting if I cause an error by asking for command line help first. For example, `plot?` causes me to get an error. Then, I cannot get any plots to launch in a window without the show command. This is quite odd. Can you get a simple list plot to launch a window if it is the first line you run after launching Sage?

calc314 gravatar imagecalc314 ( 2012-12-09 00:41:25 +0200 )edit

I can't reproduce this in the version of sage I have (5.5rc0) and I didn't face this problem when I had 5.0 installed. This can mean that it is a problem with that specific version of sage. However, not seeing even text output makes me think that there is something more fundamentally wrong with the installation. The reason why i think so is because the text output is actually doctested and so this errant behavior would have been caught before release.

ppurka gravatar imageppurka ( 2012-12-09 08:29:55 +0200 )edit

Hi Calc314 and ppurka, I tried the list_plot experiement as follows: ---------------------------------------------------------------------- | Sage Version 5.0.1, Release Date: 2012-06-10 | | Type notebook() for the GUI, and license() for information. | ---------------------------------------------------------------------- sage: list_plot([[1,2],[2,3],[3,4]]) sage: help(list_plot) sage: list_plot([[1,2],[2,3],[3,4]]) In both cases, the plot was produced. This was on a 'cold start' of sage. The problem seems to occur if I load a script because the show was seen to be necessary in the code..

PatB gravatar imagePatB ( 2012-12-09 17:11:22 +0200 )edit

Ok, here is the code that requires the .show() This is an extract from the plot() method: The commented lines do not have the show() and do not plot. list_plot(self.m,axes = 'true',aspect_ratio='automatic').show() #list_plot(self.m,axes = 'true',aspect_ratio='automatic') list_plot(self.ellipse,axes='true',aspect_ratio='automatic').show() #list_plot(self.ellipse,axes='true',aspect_ratio='automatic')

PatB gravatar imagePatB ( 2012-12-09 17:39:12 +0200 )edit

>> The problem seems to occur if I load a script because the show was seen to be necessary in the code.. Maybe you can start by debugging the script that you are loading? Probably something is setting or overriding some internal Sage variable. This shouldn't be happening though and can be considered a bug in Sage, if indeed the behavior is being changed by some script you are loading (as long as you are not overriding some Sage function). To debug try commenting out the lines carefully and try to determine which line or variable is causing the problem. The list_plot examples you gave me work here. I have been able to locate a sage-5.0rc1 installation, and I can't reproduce.

ppurka gravatar imageppurka ( 2012-12-10 04:07:23 +0200 )edit

Hi Ppurka It seems I cannot use pdb to step through the code? In that case, I will use the binary search method as you suggested to probe when list_plot() needs a show() to produce a plot. The problem may be difficult to find if list_plot from the very beginning of the script requires show()... we'll see... (I'll keep us all posted...) ... thanks for your support!

PatB gravatar imagePatB ( 2012-12-10 11:33:05 +0200 )edit

I'm not so experienced with running scripts from the command line interface. But, in the notebook, if the `list_plot` command is not the last line in a cell or function, then the plot will not display and you must use `show`. So, if your list_plot isn't the last line, I'm not surprised by the behavior.

calc314 gravatar imagecalc314 ( 2012-12-10 13:31:40 +0200 )edit

@calc314 you are correct. The list_plot will not display in the notebook if it is not the last line in the cell. @PatB can you confirm if you are working on the command line or in the notebook? Your original post says that you have the same problem in the command line. But this problem should not arise in the command line, unless you are plotting from within a function.

ppurka gravatar imageppurka ( 2012-12-10 14:18:32 +0200 )edit

Hi ppurka Both the notebook launch and the cli launch of the script from sage produce the no-show problem. If I do list_plot outside of the script I have no no-show problem

PatB gravatar imagePatB ( 2012-12-10 22:02:49 +0200 )edit

Oh, that is your problem! I don't think we have any promised behavior for plots run from a *script*. In fact, I don't even seem to get one when running the simple script `plot(x^2,(x,0,1)).show()` as `Untitled.sage`, so I don't know what's up with that. But in any case running as a script would just be like running it as a doctest, I suppose; the plot would be generated as a Sage object, but not as a graphics file. `plot(x^2,(x,0,1)).save('Hi.png')` does create an image file for me (when run as a script).

kcrisman gravatar imagekcrisman ( 2012-12-10 22:36:10 +0200 )edit

Hi kcrisman I think I have concluded the same thing. Interactive behaviour shows the plot without calling method .show() . This is similar to python interactive behaviour : the interactive python window prints evaluations such as 3**7 as 2187 without the user calling method print(). Do we have consensus: Not a bug? If so, then perhaps we should hint in the documentation that the programmer must call the show() method for plot functions like list_plot... (speaking as a newbie)

PatB gravatar imagePatB ( 2012-12-10 23:12:15 +0200 )edit

@PatB You are right. This is simply interactive python behavior. The output is shown only if the statement is the last line. For instance the following statement will only print the output of the last line (when run in a notebook cell): 2+2 2*3 Unfortunately, I think this is not mentioned explicitly in the documentation anywhere (correct me if I am wrong).

ppurka gravatar imageppurka ( 2012-12-11 02:41:47 +0200 )edit

@ppurka: maybe this should be added at least in or maybe just Are there any other places you can think of? I'd review a patch to that effect.

kcrisman gravatar imagekcrisman ( 2012-12-11 08:17:13 +0200 )edit

It should go into the introductory tutorial too IMO - within the very first 2-3 paragraphs. Like here: and the FAQ

ppurka gravatar imageppurka ( 2012-12-11 11:58:52 +0200 )edit

Or, in the first case, at least a link to the place in the programming part which talks about it, given that the tutorial starts with the *interactive* shell :)

kcrisman gravatar imagekcrisman ( 2012-12-11 22:47:49 +0200 )edit

Hi Folks, I'm curious - I could help do the documentation patch - but not sure if I'm allowed. Once that is done, I'd be happy to "Answer my own question" by citing the documentation, and then we could say the case is closed... So how can I help?

PatB gravatar imagePatB ( 2012-12-13 00:38:11 +0200 )edit