gpt4 book ai didi

gnuplot:使用字符组合的热图

转载 作者:行者123 更新时间:2023-12-04 02:38:24 25 4
gpt4 key购买 nike

我目前正在分析文本中的两个字符组合,我想使用 gnuplot 可视化热图中的频率。我的输入文件是这样的格式(COUNT代表这个组合的实际个数)

a a COUNT
a b COUNT
...
z y COUNT
z z COUNT

现在我想创建一个热图(如 first one that is shown on this site )。在 x 轴和 y 轴上,我想显示 A-Z 中的字符,即

a
b
...
z
a b ... z

我是 gnuplot 的新手,所以我尝试了 使用 2:1:3 和图像绘制“input.dat”,结果出现错误消息“无法使用空 x 绘制范围”。我天真的运行 set xrange['a':'z'] 的方法没有多大帮助。

SO 上有很多相关问题,但它们要么处理数字 x 值(例如 Heatmap with Gnuplot on a non-uniform grid),要么处理不同的输入数据格式(例如 gnuplot: label x and y-axis of matrix (heatmap) with row and column names)

所以我的问题是:将我的输入文件转换为漂亮的 gnuplot 热图的最简单方法是什么?

最佳答案

您需要将字母字符转换为整数。或许可以在 gnuplot 中以某种方式执行此操作,但它可能会很困惑。

我的解决方案是使用快速 python 脚本来转换数据文件(假设它称为 data.dat):

#!/usr/bin/env python2.7

with open('data.dat', 'r') as i:
with open('data2.dat', 'w') as o:
lines = i.readlines()
for line in lines:
line = line.split()
x = str(ord(line[0].lower()) - ord('a'))
y = str(ord(line[1].lower()) - ord('a'))
o.write("%s %s %s\n" % (x, y, line[2]))

这需要一个这样的文件:

a a 1
a b 2
a c 3
b a 4
b b 5
b c 6
c a 7
c b 8
c c 9

并将其转换为:

0 0 1
0 1 2
0 2 3
1 0 4
1 1 5
1 2 6
2 0 7
2 1 8
2 2 9

然后你可以在 gnuplot 中绘制它:

#!/usr/bin/env gnuplot

set terminal pngcairo
set output 'test.png'

set xtics ("a" 0, "b" 1, "c" 2)
set ytics ("a" 0, "b" 1, "c" 2)

set xlabel 'First Character'
set ylabel 'Second Character'

set title 'Character Combination Counts'

plot 'data2.dat' with image

以这种方式手动设置抽动有点笨拙,但效果很好。

enter image description here

关于gnuplot:使用字符组合的热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20428010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com