【问题标题】:gnuplot multiple files and adding y values single plotgnuplot 多个文件并添加 y 值单图
【发布时间】:2013-12-31 15:32:51
【问题描述】:

我有两个数据集,并希望在 gnu 绘图图上仅用一条线绘制它们。

我希望这条线代表两个数据集中 y 值的总和

这两个数据集有 x 和 y 值。

我想将两个数据集中的两个 y 值相加。

数据集中的 x 点有时相同,有时不同。

示例数据集是

数据集一

x    y
1    5
2    6
3    5
4    6

数据集二

x      y
1      1
1.7    2
3      3
7      5

【问题讨论】:

    标签: file merge dataset gnuplot add


    【解决方案1】:

    假设您可以使用cat,您可以首先使用cat fileA.txt fileB.txt 连接两个文件,并使用选项smooth frequency 将所有y 值添加到相同的x 值的绘图:

    plot '< cat fileA.txt fileB.txt' smooth frequency with lines
    

    【讨论】:

      【解决方案2】:

      对我来说,听起来你打算做的数学比我通常在 Gnuplot 中觉得舒服的要多。您是否考虑过将工具作为 python 与 pandas 一起使用?

      这可能是一个学习曲线,但这可能是学习新工具的时候了。 http://pandas.pydata.org/pandas-docs/dev/10min.html

      这会让你非常接近我想要的。

      import pandas as pd
      import matplotlib.pyplot as plt
      
      dataSet1 = pd.DataFrame( [ [1, 2], [2, 6], [3, 5], [4, 6] ], columns=list('xy'))
      dataSet2 = pd.DataFrame( [ [1, 1], [1.7, 2], [3, 3], [7, 5] ], columns=list('xy'))
      
      dataSet1 = dataSet1.set_index('x')
      dataSet2 = dataSet2.set_index('x')
      
      full_dataset =  dataSet1.add(dataSet2, fill_value=0)
      print full_dataset
      
      full_dataset.plot()
      plt.show()
      

      给你输出:

           y
      x     
      1.0  3
      1.7  2
      2.0  6
      3.0  8
      4.0  6
      7.0  5
      

      我没有立即弄清楚如何定义添加应该检查 x 列,而不是添加时的索引。我看了一点:Adding two pandas dataframesRedefining the Index in a Pandas DataFrame object

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多