强制轴标签如何包含Z矩阵的x、y输入以使其具有敏感轴

2024-04-20 13:34:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Z矩阵,当我把它插入pcolormesh时,它工作得很好,给出了下面的图。唯一的问题是轴现在显示矩阵索引。我使用的代码如下所示: enter image description here

#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看起来不错,但我的热图看起来不同。有人能照一下这个吗?非常感谢

enter image description here

    #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)

编辑1: 当我绘制boo时,y轴是均匀分布的,因为我们处理的是矩阵索引。当我绘制foo时,它们不是,因为与这些索引对应的数组值(不是数据矩阵的值,而是与数据具有相同dim但其中存储有与expt对应的值的外部y数组)不是等距排列的。问题在于,对应于数据矩阵前5个y索引的y值为1.32、3.200、3.311、3.38、3.40,它们的x值在[xmin到xmax]的整个范围内变化。但是在0和5(Y)之间有一个巨大的红色斑点,一直水平移动到xlim的末尾。很明显,有什么地方出了问题,但却不知道是什么


Tags: datatitlenpargsplt矩阵filenamecmap
1条回答
网友
1楼 · 发布于 2024-04-20 13:34:26

我不是100%清楚你想做什么,但是如果你想用Boo来绘制数据,但是使用了一些不同的刻度标签,那么我认为对下面这个自包含示例的修改可能会对你有用

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教程on labeling heatmaps来获得帮助

相关问题 更多 >