Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks for sharing your code! You figured out all the hard stuff for us. As I was working with it, I noticed a few little things that didn't look quite right.</p> <ol> <li><p>[FIX #1] The axis tics weren't lining up like I would expect (i.e., in your example above, you should be able to draw a vertical and horizontal line through any point across all plots and the lines should cross through the corresponding point in the other plots, but as it sits now this doesn't occur.</p></li> <li><p>[FIX #2] If you have an odd number of variables you are plotting with, the bottom right corner axes doesn't pull the correct xtics or ytics. It just leaves it as the default 0..1 ticks.</p></li> <li><p>Not a fix, but I made it optional to explicitly input <code>names</code>, so that it puts a default <code>xi</code> for variable i in the diagonal positions.</p></li> </ol> <p>Below you'll find an updated version of your code that addresses these two points, otherwise preserving the beauty of your code.</p> <pre><code>import itertools import numpy as np import matplotlib.pyplot as plt def scatterplot_matrix(data, names=[], **kwargs): """ Plots a scatterplot matrix of subplots. Each row of "data" is plotted against other rows, resulting in a nrows by nrows grid of subplots with the diagonal subplots labeled with "names". Additional keyword arguments are passed on to matplotlib's "plot" command. Returns the matplotlib figure object containg the subplot grid. """ numvars, numdata = data.shape fig, axes = plt.subplots(nrows=numvars, ncols=numvars, figsize=(8,8)) fig.subplots_adjust(hspace=0.0, wspace=0.0) for ax in axes.flat: # Hide all ticks and labels ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) # Set up ticks only on one side for the "edge" subplots... if ax.is_first_col(): ax.yaxis.set_ticks_position('left') if ax.is_last_col(): ax.yaxis.set_ticks_position('right') if ax.is_first_row(): ax.xaxis.set_ticks_position('top') if ax.is_last_row(): ax.xaxis.set_ticks_position('bottom') # Plot the data. for i, j in zip(*np.triu_indices_from(axes, k=1)): for x, y in [(i,j), (j,i)]: # FIX #1: this needed to be changed from ...(data[x], data[y],...) axes[x,y].plot(data[y], data[x], **kwargs) # Label the diagonal subplots... if not names: names = ['x'+str(i) for i in range(numvars)] for i, label in enumerate(names): axes[i,i].annotate(label, (0.5, 0.5), xycoords='axes fraction', ha='center', va='center') # Turn on the proper x or y axes ticks. for i, j in zip(range(numvars), itertools.cycle((-1, 0))): axes[j,i].xaxis.set_visible(True) axes[i,j].yaxis.set_visible(True) # FIX #2: if numvars is odd, the bottom right corner plot doesn't have the # correct axes limits, so we pull them from other axes if numvars%2: xlimits = axes[0,-1].get_xlim() ylimits = axes[-1,0].get_ylim() axes[-1,-1].set_xlim(xlimits) axes[-1,-1].set_ylim(ylimits) return fig if __name__=='__main__': np.random.seed(1977) numvars, numdata = 4, 10 data = 10 * np.random.random((numvars, numdata)) fig = scatterplot_matrix(data, ['mpg', 'disp', 'drat', 'wt'], linestyle='none', marker='o', color='black', mfc='none') fig.suptitle('Simple Scatterplot Matrix') plt.show() </code></pre> <p>Thanks again for sharing this with us. I have used it many times! Oh, and I re-arranged the <code>main()</code> part of the code so that it can be a formal example code or not get called if it is being imported into another piece of code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload