python散点图矩阵怎么做_matplotlib中是否有生成散点图矩阵的函数?

一般来说,matplotlib通常不包含在多个轴对象(在本例中是子块)上操作的绘图函数。我们的期望是编写一个简单的函数,可以根据需要将所有内容串在一起。

我不太确定您的数据是什么样子,但是从头开始构建一个函数来完成这一点非常简单。如果您总是要使用结构化或rec阵列,那么您可以简化这一点。(即,总是有一个与每个数据系列相关联的名称,因此您可以省略指定名称的操作。)

例如:import itertools

import numpy as np

import matplotlib.pyplot as plt

def 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()

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.05, wspace=0.05)

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)]:

axes[x,y].plot(data[x], data[y], **kwargs)

# Label the diagonal subplots...

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)

return fig

main()

QuxDD.png

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值