强制轴标签-如何为Z矩阵包括x,y输入以使其具有灵敏的轴 [英] Forcing axis labels - How to include x,y inputs for a Z matrix to have sensible axes

查看:78
本文介绍了强制轴标签-如何为Z矩阵包括x,y输入以使其具有灵敏的轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Z矩阵,当我将其插入pcolormesh时,它可以完美工作并给出以下图.唯一的问题是轴现在显示矩阵索引.我使用的代码如下:

I have a Z matrix and when I plug it into pcolormesh, it works perfectly and gives me the following plot. The only problem is that the axes now displays the matrix indices. The code that I used make it is given below:

#boo - most of the parameters like title,xyz labels, filename comes from command line
data = np.loadtxt((args.data),dtype=float, comments="#")
cmap = plt.get_cmap('bwr')
fig, ax0 = plt.subplots()
divnorm = colors.DivergingNorm(vmin=np.amin(data), vcenter=0, vmax=np.amax(data))
im0 = ax0.pcolormesh(data,norm=divnorm, cmap=cmap)
fig.colorbar(im0,ax=ax0)
ax0.set_title(str(title))
plt.xlabel(str(xlabel))
plt.ylabel(str(ylabel))
filename = str(prefix) + "."+ str(fileformat)
plt.savefig(filename)

我想将x轴缩放0.1倍(由于没有看到解决方法,因此手动完成了此操作),并将y轴设置为相对于另一个数组改变(注意:而不是操纵Z矩阵,而是使用物理上有意义的实验值数组-在此为sortData-与矩阵索引相对应).我按如下方式更改了代码-x轴和yaxis看起来都不错,但我的热图却有所不同.有人可以对此有所启发吗?非常感谢

I wanted to rescale the x-axis by a factor of 0.1 (ended up doing it manually since I did not see a workaround) and set the y-axis to change with respect to another array (Note that: I'm not manipulating Z matrix instead I'm using a physically meaningful experimental value array - here, sortData - corresponding to matrix indices). I changed my code as follows - x axis and yaxis seem alright but my heatmap looks different. Can someone shine some light on this? Many Thanks

    #foo
    Data = np.loadtxt((args.data),dtype=float, comments="#")
    sort = np.loadtxt((args.sortData),dtype=float, comments="#")

    fig, ax0 = plt.subplots()
    cmap = plt.get_cmap('bwr')
    divnorm = colors.DivergingNorm(vmin=np.amin(Data), vcenter=0, vmax=np.amax(Data))
    #  im0 = ax0.pcolormesh(Data,norm=divnorm, cmap=cmap)
    #  ax0.set_xscale(1, "linear")
    x = np.arange(0.0,10.6,0.1)  # need to set the ticks manually
    y = sort[:,1]
    X,Y = np.meshgrid(x,y)
    Z=z.reshape(len(y),len(x))
   im0 = ax0.pcolormesh(X,Y,Data,norm=divnorm, cmap=cmap)#, extent=[x.min(), x.max(), y.min(), y.max()])
   #im0 = ax0.pcolormesh(x,y,Data,norm=divnorm, cmap=cmap)#, extent=[x.min(), x.max(), y.min(), y.max()])

    cbar = fig.colorbar(im0,ax=ax0)

    if args.zlabel !=None:
         cbar.ax.set_ylabel(str(args.zlabel))
    ax0.set_title(str(args.title))
    plt.xlabel(str(args.xlabel))
    plt.ylabel(str(args.ylabel))
    filename = str(args.prefix) + "."+ str(args.fileformat)
    plt.savefig(filename)

当我绘制boo时,由于我们要处理矩阵索引,因此y轴是均匀间隔的.当我绘制foo时,它们不是那样的,因为对应于这些索引的数组值(不是Data矩阵的数组值,而是与Data一样暗但具有存储在其中的值对应于expt的外部y数组)的间距不相等.问题是与数据矩阵的前5个y索引相对应的y值是1.32,3.200,3.311,3.38,3.40,并且它们的x值在[xmin到xmax]范围内变化.但是在0到5(Y)之间有一个巨大的红色斑点,一直沿水平方向一直延伸到xlim的尽头.显然有些事情是错误的,但无法弄清楚它是什么.

EDIT 1: When I plot the boo, y-axis is uniformly spaced since we are dealing with the matrix indices. When I plot foo, they are not since the array values corresponding to these indices ( not that of Data matrix but the external y array that is of same dim as Data but has values stored in it corresponding to expt) are not equally spaced. The problem is The y values corresponding to The first 5 y indexes of Data matrix are 1.32, 3.200, 3.311, 3.38, 3.40 and their x values change throughout the range [xmin to xmax]. But there's a giant blob of red thing between 0 and 5 (Y) that goes horizontally all the way till the end of xlim. Clearly something is wrong but can't figure out what it is.

推荐答案

我不确定您要做什么,但如果您要在Boo中绘制数据,但要设置一些不同的设置,我并不能100%清楚的刻度标签,那么我认为对以下独立示例的修改可能对您有用.

I'm not 100% clear on what you're trying to do, but if you're trying to plot the data in Boo but with some different set of tick labels then I think a modification of the following self-contained example would probably work for you.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors

title = 'Foo'
xlabel = 'X'
ylabel = 'Y'


rv = np.random.rand(100)  # uniform random vector
data = rv[:,None] - rv.T  # pairwise diffs

cmap = plt.get_cmap('bwr')
fig, ax = plt.subplots()
divnorm = colors.DivergingNorm(vmin=np.amin(data), vcenter=0, vmax=np.amax(data))
im0 = ax.pcolormesh(data, norm=divnorm, cmap=cmap)
fig.colorbar(im0, ax=ax)

# do tick labeling stuff here
nticks = 5 
x_tick_pos = np.linspace(0,100,nticks)
y_tick_pos = np.linspace(0,100,nticks)
ax.set_xticks(x_tick_pos)
ax.set_yticks(y_tick_pos)
xtick_labels = [str(x) for x in np.linspace(0, 10, nticks)] # can be any list of strings
ytick_labels = [str(y) for y in np.linspace(0, 10, nticks)] # len must match nticks
ax.set_xticklabels(xtick_labels)
ax.set_yticklabels(ytick_labels)
ax.set_title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)

plt.show()

请注意,如果您想做更奇特的事情,例如旋转刻度线标签以便于阅读,可以通过查看matplotlib教程

Note that if you want to do fancier things, like have the tick labels rotated so that they can be easier to read, you might be aided by checking out the matplotlib tutorial on labeling heatmaps.

这篇关于强制轴标签-如何为Z矩阵包括x,y输入以使其具有灵敏的轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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