matshow的问题 [英] Issues with matshow

查看:66
本文介绍了matshow的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib matshow一起显示矩阵和相关的矢量数据.

vec_data = np.array([[ 0.,  1.,  1.,  1.,  0.,  1.,  1.,  0.,  0.,  0.]])

mat_data = np.array([
       [ 0. ,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0. ],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  1. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0.5,  0. ,  0.5,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0.1,  0.1,  0.1,  0.1,  0. ,  0.1,  0.1,  0.1,  0. ,  0.1],
       [ 0. ,  0. ,  1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0. ,  0.1,  0.1],
       [ 0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0. ,  0. ],
       [ 0.1,  0.1,  0.1,  0.1,  0. ,  0.1,  0.1,  0.1,  0.1,  0. ]])

fig, axes = plt.subplots(2,1,figsize=(4,4),sharey=False,sharex=True,gridspec_kw = {'height_ratios':[25,1]})
axes[0].matshow(mat_data)
axes[1].matshow(vec_data)
axes[1].tick_params(direction='out', length=6, width=0)
axes[1].set_yticklabels([''])
axes[1].set_xlabel('vector')

结果图像如下:

这里的问题是,当将这两个matshow图像放在一起时,会使第一张图像的ylim混乱:应该显示0到9之间的值,但是只显示0.5到8.5之间的范围.如果我使用命令

单独绘制图像

plt.matshow(mat_data)

我获得了正确的ylim所需的图像.

任何人都知道导致此问题的原因以及如何解决该问题?我尝试使用

axes[0].set_ylim([-0.5,9.5])

但它不起作用.

PS:我使用了关键字gridspec_kw = {'height_ratios':[25,1]},以便将矢量显示为矢量-否则将显示为具有空白值的矩阵,如下所示. /p>

plt.subplots使用参数sharex = True,以使向量和矩阵对齐.没有参数,该图将如下所示

但请注意,ylim的问题已消失-因此,争论可能是导致此问题的主要原因.我想我是否可以找到另一种无需使用"sharex = True"来对齐两个图像的方法就可以解决此问题.

解决方案

为子图使用sharex=True会过度约束系统. Matplotlib将因此释放绘图限制,以便能够显示具有给定规格的绘图.

解决方案是使用sharex=False(默认设置).然后,高度比例需要匹配图像的尺寸,即

fig, axes = plt.subplots(2,1,figsize=(4,4),sharey=False,sharex=False,
                    gridspec_kw = {'height_ratios':[mat_data.shape[0],vec_data.shape[0]]})

完整示例:

import numpy as np
import matplotlib.pyplot as plt

vec_data = np.array([[ 0.,  1.,  1.,  1.,  0.,  1.,  1.,  0.,  0.,  0.]])
mat_data = np.random.choice([0,.1,.5,1], size=(10,10))

fig, axes = plt.subplots(2,1,figsize=(4,4),sharey=False,sharex=False,
                    gridspec_kw = {'height_ratios':[mat_data.shape[0],vec_data.shape[0]]})
axes[0].matshow(mat_data)
axes[1].matshow(vec_data)
axes[1].tick_params(direction='out', length=6, width=0)
axes[1].set_yticklabels([''])
axes[1].set_xlabel('vector')

plt.show()

I am trying to show a matrix and a related vector data together using matplotlib matshow.

vec_data = np.array([[ 0.,  1.,  1.,  1.,  0.,  1.,  1.,  0.,  0.,  0.]])

mat_data = np.array([
       [ 0. ,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0. ],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  1. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0.5,  0. ,  0.5,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0.1,  0.1,  0.1,  0.1,  0. ,  0.1,  0.1,  0.1,  0. ,  0.1],
       [ 0. ,  0. ,  1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0. ,  0.1,  0.1],
       [ 0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0. ,  0. ],
       [ 0.1,  0.1,  0.1,  0.1,  0. ,  0.1,  0.1,  0.1,  0.1,  0. ]])

fig, axes = plt.subplots(2,1,figsize=(4,4),sharey=False,sharex=True,gridspec_kw = {'height_ratios':[25,1]})
axes[0].matshow(mat_data)
axes[1].matshow(vec_data)
axes[1].tick_params(direction='out', length=6, width=0)
axes[1].set_yticklabels([''])
axes[1].set_xlabel('vector')

The resulting image would be as follows:

The issue here is that when putting these two matshow images together mess up the ylim of the first image: it is supposed to show the values from 0 to 9, but it only shows a range 0.5 to 8.5. If I plot the image alone with the command

plt.matshow(mat_data)

I get the desired image with correct ylim.

Anybody knows what causes the issue and how I can fix it? I tried to use

axes[0].set_ylim([-0.5,9.5])

but it does not work.

P.S.: I have used the keyword gridspec_kw = {'height_ratios':[25,1]} so that the vector is shown as a vector--otherwise it will be shown as a matrix with blank values like the following.

The argument sharex = True is used for plt.subplots in order for the vector and the matrix to be aligned. Without the argument, the graph will be like the following

but note there that the issue with the ylim is gone--so it is possible that the argument is the main cause of this issue. I guess if I can find another way to align the two images without using "sharex = True" can solve this issue.

解决方案

Using sharex=True for the subplots overconstrains the system. Matplotlib will hence free the plot limits to be able to show the plot with the given specifications.

The solution would be to use sharex=False (the default). Then the height-ratios need to match the dimensions of the images, i.e.

fig, axes = plt.subplots(2,1,figsize=(4,4),sharey=False,sharex=False,
                    gridspec_kw = {'height_ratios':[mat_data.shape[0],vec_data.shape[0]]})

Complete example:

import numpy as np
import matplotlib.pyplot as plt

vec_data = np.array([[ 0.,  1.,  1.,  1.,  0.,  1.,  1.,  0.,  0.,  0.]])
mat_data = np.random.choice([0,.1,.5,1], size=(10,10))

fig, axes = plt.subplots(2,1,figsize=(4,4),sharey=False,sharex=False,
                    gridspec_kw = {'height_ratios':[mat_data.shape[0],vec_data.shape[0]]})
axes[0].matshow(mat_data)
axes[1].matshow(vec_data)
axes[1].tick_params(direction='out', length=6, width=0)
axes[1].set_yticklabels([''])
axes[1].set_xlabel('vector')

plt.show()

这篇关于matshow的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆