关于散点图矩阵的 cmets 也启发了我尝试类似的方法。散点图矩阵并不是我想要的,但我从@lbn-plus-1 建议的@tisimst's answer 中获取了代码并对其进行了一些调整,如下所示:
import itertools
import numpy as np
import matplotlib.pyplot as plt
def scatterplot_matrix(data, names=[], **kwargs):
"""Plots a pcolormesh matrix of subplots. The two first dimensions of
data are plotted as a mesh of values, one for each of the two last
dimensions of data. Data must thus be four-dimensional and results
in a matrix of pcolormesh plots with the number of rows equal to
the size of the third dimension of data and number of columns
equal to the size of the fourth dimension of data. Additional
keyword arguments are passed on to matplotlib\'s \"pcolormesh\"
command. Returns the matplotlib figure object containg the subplot
grid.
"""
assert data.ndim == 4, 'data must be 4-dimensional.'
datashape = data.shape
fig, axes = plt.subplots(nrows=datashape[2], ncols=datashape[3], 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 ii in xrange(datashape[2]):
for jj in xrange(datashape[3]):
axes[ii,jj].pcolormesh(data[:,:,ii,jj], **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)
data = np.random.random([10] * 4)
fig = scatterplot_matrix(data,
linestyle='none', marker='o', color='black', mfc='none')
fig.suptitle('Simple Scatterplot Matrix')
plt.show()
我把上面的模块保存为datamatrix.py,使用如下:
import datamatrix
import brewer2mpl
colors = brewer2mpl.get_map('RdBu', 'Diverging', 11).mpl_colormap
indicator = np.ma.masked_invalid(-np.sign(data1 - data2)) # Negated because the 'RdBu' colormap is the wrong way around
fig = datamatrix.scatterplot_matrix(indicator, cmap = colors)
plt.show()
brewer2mpl 和颜色映射的东西可以省略 - 这只是我正在玩弄的一些颜色。结果如下图:
矩阵的“外部”维度是两个参数(data1 和data2 的最后两个维度)。矩阵内的每个pmeshcolor 图都是一个“出现图”,有点类似于this answer 中的图,但是对于每对参数来说都是一个二进制图。一些图底部的白线是相等的区域。每个右上角的白点是数据中的nan 值。