Seaborn: sns.PairGrid.add_legend() not leading to display of legend

Created on 3 Oct 2014  路  7Comments  路  Source: mwaskom/seaborn

I am plotting a PairGrid with a moderate number of data points (1700), one variable assigned to hue. The hue is being mapped correctly, as it shows up on the plotted points, but when I call add_legend(), only the title of the legend is displaying. I'm pretty sure expected behaviour would be a colourbar with labels indicating the data range across the hues?
(see no. 15 in http://nbviewer.ipython.org/github/mtbannister/astroquery/blob/master/astroquery/mpc/example_plots/datavis_plot_tool_comparison_tnos.ipynb )

question

Most helpful comment

Unfortunately it does mean remapping the data into an 0-1 range, otherwise matplotlib will complain bitterly

I'm not sure what you're seeing, but it works fine for me if I generate c from randn instead of rand:

iris = sns.load_dataset("iris")
color = np.random.randn(len(iris))
g = sns.PairGrid(iris, size=2.5)
g.map(plt.scatter, c=colors, cmap="YlGn")
cax = g.fig.add_axes([.98, .4, .01, .2])
plt.colorbar(cax=cax)
g.savefig("/Users/mwaskom/Desktop/iris_colors.png", bbox_inches="tight")

iris_colors

All 7 comments

You need to call add_legend _after_ calling map. Also the hue variable is really only for coloring by categorical levels, it looks like you're trying to use it with a continuous variable here which might "work" but will take a long time (as you note) and will give surprising results when the legend actually works.

Thanks.
Yeah, the results are that it crashes:
WARNING:astropy:FormatterWarning: Exception in image/png formatter: width and height must each be below 32768
WARNING: FormatterWarning: Exception in image/png formatter: width and height must each be below 32768 [IPython.core.formatters]

Would the most appropriate solution be to bin the continuous variable, create a column in the DataFrame filled with each bin, and force-feed that column to the hue? Just trying to get the feel of how to best use PairGrid.

Yes, I would bin the variable and use that as hue. See #315 for preliminary thoughts on how to better handle continuous coloring in the context of axis grids.

Actually I totally lied. This is very much possible and easy in the current framework (for PairGrid at least; it's a bit trickier with FacetGrid). Here's an example with the iris dataset. The only annoying part is getting a useful colorbar. If you just call plt.colorbar it will eat space from the bottom right axes. This might not be the easiest way to put it out of the way, but it does work.

iris = sns.load_dataset("iris")
color = np.random.rand(len(iris))
g = sns.PairGrid(iris, size=2.5)
g.map(plt.scatter, c=colors, cmap="YlGn")
cax = g.fig.add_axes([.98, .4, .01, .2])
plt.colorbar(cax=cax)
g.savefig("/Users/mwaskom/Desktop/iris_colors.png", bbox_inches="tight")

iris_colors

Hmm, that sorta works. I like the use of the 0-1 range that plt.scatter(c= ) accepts for its greyscale argument to spoof the data assigned to the hue= argument. Unfortunately it does mean remapping the data into an 0-1 range, otherwise matplotlib will complain bitterly, and then manually hacking the colourbar labels, less ideal.

Unfortunately it does mean remapping the data into an 0-1 range, otherwise matplotlib will complain bitterly

I'm not sure what you're seeing, but it works fine for me if I generate c from randn instead of rand:

iris = sns.load_dataset("iris")
color = np.random.randn(len(iris))
g = sns.PairGrid(iris, size=2.5)
g.map(plt.scatter, c=colors, cmap="YlGn")
cax = g.fig.add_axes([.98, .4, .01, .2])
plt.colorbar(cax=cax)
g.savefig("/Users/mwaskom/Desktop/iris_colors.png", bbox_inches="tight")

iris_colors

The right effect is occurring now with binned (now categorical!) data, so I'll leave it for now.
Thanks again for the help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

btyukodi picture btyukodi  路  3Comments

JanHomann picture JanHomann  路  3Comments

chanshing picture chanshing  路  3Comments

sofiatti picture sofiatti  路  4Comments

ConstantinoSchillebeeckx picture ConstantinoSchillebeeckx  路  4Comments