gnuplot:用行和列名称标记矩阵(热图)的 x 和 y 轴 [英] gnuplot: label x and y-axis of matrix (heatmap) with row and column names

查看:45
本文介绍了gnuplot:用行和列名称标记矩阵(热图)的 x 和 y 轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 gnuplot 完全陌生,在谷歌搜索后没有找到可行的解决方案.

I'm absolutely new to gnuplot and did not find a working solution after googling.

我有一个看起来像这样的数据矩阵:

I have a data matrix looking something like this:

  A B C D E
A 0 2 3 4 5
B 6 0 8 9 0
C 1 2 0 4 5
D 6 7 8 0 0
E 1 2 3 4 0

我想要做的是用 plot 'result.csv' matrix with image 绘制热图,其中 x 轴用 A-E 标记,y 轴用 A-E 标记.这个矩阵的大小并不总是相同,但行数总是等于列数,并且总是被标记.

What I would like to do is plotting a heatmap with plot 'result.csv' matrix with image, with the x-axis labeled with A-E and the y-axis labeled with A-E. This matrix does not always have the same size, but the number of row always equals the number of cols and it's always labeled.

有人可以帮我解决这个问题吗?我想我必须使用 using 命令,但到目前为止,这对我来说似乎是完整的伏都教...

Could someone help me out with this? I guess I have to use the using command, but up to now this appears like complete voodoo to me...

非常感谢!

推荐答案

这实际上是一个 doosie,如果没有 *nix shell 魔法,我无法实现它.

This one is actually a doosie and I can't make it happen without some *nix shell magic.

首先,我们要得到 x tic 标签和 y tic 标签:

First, we want to get the x tic labels and y tic labels:

XTICS="`awk 'BEGIN{getline}{printf "%s ",$1}' test.dat`"
YTICS="`head -1 test.dat`"

此时,XTICS 是字符串F G H I J",YTICS 是字符串A B C D E".

At this point, XTICS is the string "F G H I J" and YTICS is the string "A B C D E".

现在,我们要通过迭代来设置 xtics:

Now, we want to set the xtics by iteration:

set for [i=1:words(XTICS)] xtics ( word(XTICS,i) i-1 )
set for [i=1:words(YTICS)] ytics ( word(YTICS,i) i-1 )

我们使用了 2 个 gnuplot 内置函数(wordwords).words(string) 计算给定字符串中有多少个单词(一个单词是一个由空格分隔的字符序列).word(string,n) 返回字符串中的第 n 个单词.

We've used 2 gnuplot builtin functions (word and words). words(string) counts how many words there are in the given string (a word is a character sequence separated by spaces). word(string,n) returns the n'th word in the string.

现在,我们可以绘制您的数据文件......唯一的问题是matrix 想要使用数据文件中的所有行和列.您可能可以通过使用 every 关键字来减少实际读取的行/列,但我不知道如何在矩阵文件上执行此操作——我认为保留它可能更容易依赖于 shell 实用程序(awksed)

Now, we can plot your datafile ... The only problem is that matrix wants to use all rows and columns in your datafile. You might be able to cut down the rows/columns actually read by using the every keyword, but I don't know how to do that on matrix files -- and I think it's probably easier to just keep on relying on shell utilities (awk and sed)

plot "<awk '{$1=""}1' test.dat | sed '1 d'" matrix w image
#######^ replace the first field with nothing
################################## ^ delete first line

现在你的情节(希望如此)看起来像你想要的那样.

And now your plot (hopefully) looks the way you want it to.

另请注意,由于我们使用了迭代,此脚本仅适用于 gnuplot 4.3 或更高版本 -- 由于当前稳定版是 4.6,希望没问题.

这篇关于gnuplot:用行和列名称标记矩阵(热图)的 x 和 y 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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