Seaborn: Should imshow display a grid by default when under seaborn style?

Created on 17 Dec 2014  路  7Comments  路  Source: mwaskom/seaborn

Currently, if seaborn is imported, imshow will overlay a white grid over its image:
$ python -c 'from pylab import *; import seaborn; imshow(rand(20, 20)); show()'
I feel that if one is calling directly imshow, rather than some higher level method in seaborn, it is more likely that one is just trying to plot some real image data and does not want the grid.
Is there a way to avoid adding the grid for such plots? (other than wrapping each call using set_style...)

Most helpful comment

You can also use axes_style in a with statement, which will at least save you one line of code per plot:

with sns.axes_style("white"):
    plt.imshow(x)

All 7 comments

Well, that's expected behavior. I don't know why matplotlib draws imshow the way it does, but one way to avoid this is to set the zorder of the image to something greater than 0, should should put it over the grid.

You can also use axes_style in a with statement, which will at least save you one line of code per plot:

with sns.axes_style("white"):
    plt.imshow(x)

Yes, I see that that's basically matplotlib's behavior and you can't do much about it (I'd guess the lines of the grid obey normal zorder and thus come on top of images... which I usually don't notice as I usually don't have a grid on by default).
What about making axes_style a contextdecorator, making your example even shorter: sns.axes_style("white")(plt.imshow)(x)? This would simply require adding a __call__ method to AxesStyle and PlottingContext, taking a function and returning a wrapper function that calls the wrappee under the proper context. (In practice, this would be useful for me as I need to put long callback functions under the proper context, so I'd save an entire indendation level.)

Actually you only need to have the command that creates the Axes under the context manager, as all the rc parameters end up as attributes of that object. So it would be more like

with sns.axes_style("white"):
    f, ax = plt.subplots()
ax.imshow(x)

For images the dark style might be more convenient as you won't end up with a black frame around the image.

Hum, I see that ax.clear (which I wanted to call every now and then) also clears the stored style, so that won't cut it. (In fact the style at plotting time doesn't matter at all, it's just saved when the axes are created.) So I guess I'll open a PR for matplotlib to add an option to ax.clear that keeps the stored style.
Thanks for the tip re: dark style.

It seems like the right thing would be for matplotlib's imshow to turn grids off by default (since it seems rare you'd ever want them when plotting an image). Anyway, just an annoyance, and nota bug in seaborn, so closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Bercio picture Bercio  路  3Comments

sungshine picture sungshine  路  3Comments

ConstantinoSchillebeeckx picture ConstantinoSchillebeeckx  路  4Comments

TDaltonC picture TDaltonC  路  3Comments

btyukodi picture btyukodi  路  3Comments