【问题标题】:Gnuplot command to matplotlib到 matplotlib 的 Gnuplot 命令
【发布时间】:2014-05-09 02:25:23
【问题描述】:

我正在编辑一些在 Fortran 上运行并使用 gnuplot 的旧软件。我正在尝试将一些部分移植到 python 中,我有点困惑。

我有一个飓风数据的矩阵文件,它使用以下命令进行绘图:

plot 'file.txt' matrix with image 1:999:1:480 w l t       ""

我不完全理解 gnuplot 命令,所以当我将 plot 函数与 matplotlib 一起使用时,我得到了完全不同的东西。

有人知道我如何在 python 中以同样的方式绘制吗?

【问题讨论】:

  • 此命令不起作用:您指定了两种绘图样式with imagew l(表示with lines)。此外,您可能在1:999:1:480 之前缺少every。请更正绘图命令。

标签: python numpy matplotlib gnuplot


【解决方案1】:

我同意 Christoph 的观点:您可能的 gnuplot 命令行接近于:

plot 'file.txt' matrix with image every 1:999:1:480  t  ""

plot 规范 with imagewith line 存在冲突。
Gnuplot 是懒惰的爱好者。或者你讨厌它或者你喜欢它:-)

你总能找到缩短的命令:

plot  ---> pl  
with  ---> w  
lines ---> l  
title ---> t  
and so on...

在 Python 中,您可以尝试使用类似的东西(如果它错过了命令行中的 every 字,它仍然会过滤数据...)

#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# Read in data from an ASCII data table
data = np.genfromtxt('data.txt')

ax.imshow(data, cmap=plt.cm.gray, interpolation='nearest')
#ax.imshow(data, cmap=plt.cm.gray)
ax.set_title('My Title')
plt.show()

它再现了help image的gnuplot示例的效果
data.txt 文件中我放了与示例相同的数据

5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 1 2 4 3

【讨论】:

  • 作为注释的边缘:我尝试重现 Gnuplot 的原始行为。如果没有interpolation,图像结果会有所不同。当您首先迁移代码时,您必须重现旧代码。
  • 挑剔:请不要from pylab import *。在这个片段中,您只使用来自 numpy 的genfromtxt
  • 修改代码:所以它需要的更少... @Zhenya: nitpick*nitpick 正确的 之后你要写data=np.genfromtxt('data.txt')
猜你喜欢
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
相关资源
最近更新 更多