2015-11-12 47 views
0

我有一个txt文件contatning点,这是看起来像以下:如何绘制2D图在linux

##x1 y1 x2 y2 
123 567 798 900 
788 900 87 89 
.... 

我想提请其使用的gnuplot在.txt文件的链接线每对2D图。

EDIT1:

我觉得这Draw points and lines in gnuplot,但我不知道怎么在我的情况下使用gnuplot的。

EDIT2:

没有为图类型没有限制。 当我看到here我可以使用下面的设置终端PNG:

set terminal png 

对于线风格,我觉得这个例子here

set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1.5 

,也没有必要警示标签轴。

+0

并且将这个图是一个PDF,或PNG或JPEG?线条是什么颜色?他们应该有多厚?轴是否需要标签?最后,你到目前为止尝试过什么? –

+0

我已编辑我的问题。 – user3806649

+0

文件中可能有多少行 - 大致?你知道x和y的最大可能值 - 都是x,并且所有y都在1000以下,也许 –

回答

2

更新

在你对这个答案的底线的数量和最大的x,y的尺寸,我原有的基础ImageMagick的的方式答复的光线显然不是正确的你的具体问题。但是,我会留给其他人看,因为对于多达几十条线路来说,这是完全正确的。我现在提供更合适的gnuplot版本。

gnuplot的版本

如果你想与gnuplot做到这一点,它会是这个样子:

set terminal png size 1000,1000 
set output 'result.png' 
unset xtics 
unset ytics 
unset border 
plot 'lines.txt' using 1:2:($3-$1):($4-$2) with vectors nohead notitle 

如果保存在一个文件中卡莱plot.cmd,然后你可以运行它与

gnuplot < plot.cmd 

enter image description here

如果你想箭头,使用这样的变体:

set terminal png size 1000,1000 
set output 'result.png' 
set style arrow 1 heads filled size screen 0.03,15,45 ls 1 
unset xtics 
unset ytics 
unset border 
plot 'lines.txt' using 1:2:($3-$1):($4-$2) with vectors arrowstyle 1 notitle 

enter image description here

Magick ++和C++回答

我决定制定一个Magick ++和C++的答案,只是为了好玩。代码如下所示 - 编译命令显示在顶部的注释中。

//////////////////////////////////////////////////////////////////////////////// 
// sample.cpp 
// Mark Setchell 
// 
// ImageMagick Magick++ sample code 
// 
// Compile with: 
// g++ sample.cpp -o sample $(Magick++-config --cppflags --cxxflags --ldflags --libs) 
//////////////////////////////////////////////////////////////////////////////// 
#include <Magick++.h> 
#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
#include <vector> 

using namespace std; 
using namespace Magick; 

int main(int argc,char **argv) 
{ 
    InitializeMagick(*argv); 

    // Create an image object, scaled by a factor of 100 to speed it up ! 
    float scale=100.0; 
    Image image("650x650","white"); 

    // Construct drawing list 
    std::list<Magick::Drawable> drawList; 

    // Initial settings, blue lines 1 pixel thick 
    drawList.push_back(DrawableStrokeColor("blue")); 
    drawList.push_back(DrawableStrokeWidth(1)); 

    // Read in lines from file, expected format "x1 y1 x2 y2" 
    int lineno=0; 
    std::ifstream infile("lines.txt"); 
    std::string line; 
    while (std::getline(infile, line)) 
    { 
     std::istringstream iss(line); 
     int x1,y1,x2,y2; 
     iss >> x1; 
     iss >> y1; 
     iss >> x2; 
     iss >> y2; 
     x1 = int(x1/scale); 
     y1 = int(x2/scale); 
     x2 = int(y1/scale); 
     y2 = int(y2/scale); 
     cout << "Line: " << ++lineno << " " << x1 << "," << y1 << " " << x2 << "," << y2 << endl; 
     // Add this point to the list of lines to draw 
     drawList.push_back(DrawableLine(x1,y1,x2,y2)); 
    } 

    // Draw everything using completed drawing list 
    image.draw(drawList); 

    // Write the image to a file 
    image.write("result.png"); 

    return 0; 
} 

我产生1000行的随机试验数据用Perl这样的:

perl -E 'for($i=0;$i<1000;$i++){printf("%d %d %d %d\n",int rand 65000,int rand 65000, int rand 65000, int rand 65000);}' > lines.txt 

结果看起来是这样的:

enter image description here

原来的答案

你凑合ld也可以很容易地使用ImageMagick,它已经安装在大多数Linux发行版中。其实有只有4行代码在下面 - 其余的所有注释:

#!/bin/bash 

# Create the output image, 1000x1000 pixels say 
convert -size 1000x1000 xc:pink result.png 

# Suppressing lines that have a hash (#) at the start, read in the file "lines.txt" 
grep -v "^#" lines.txt | while read x1 y1 x2 y2; do 

    echo Read line $x1,$y1 $x2,$y2 

    # Tell ImageMagick to draw the line on the image 
    convert result.png -stroke blue -strokewidth 5 -draw "line $x1,$y1 $x2,$y2" result.png 
done 

enter image description here

输出

Read line 123,567 798,900 
Read line 788,900 87,89 
+0

如何标记积分? – user3806649

+0

哪点?用什么标记? –

+0

我想用circle或其他东西来显示每一行的开始和结束 – user3806649

1

你也可以做到这一点使用rsvg-convert(从的librsvg )或svg2png。这两种程序都希望SVG格式的inout文件将其呈现为PNG文件。所以,你需要你的

123 567 798 900 
788 900 87 89 

转换成这种类型的事情

<svg xmlns="http://www.w3.org/2000/svg" version="1.1"> 
    <line x1="123" y1="567" x2="798" y2="900" stroke="blue" stroke-width="1"/> 
    <line x1="788" y1="900" x2="87" y2="89" stroke="blue" stroke-width="1"/> 
</svg> 

这是可以做到很轻松了一点点awk这样的脚本:

awk ' 
    BEGIN{ printf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n") } 

    { 
    x1=$1; y1=$2; x2=$3; y2=$4; 
    printf("<line x1=\"" x1 "\" y1=\"" y1 "\" x2=\"" x2 "\" y2=\"" y2 "\" stroke=\"blue\" stroke-width=\"1\"/>\n") 
    } 

    END{ printf("</svg>\n") }' points.txt 

然后,您可以将其输出泵送到我上面提到的两个程序中的任一个:

awk ... | rsvg-convert -b \#ffff00 > result.png 

enter image description here