如何在Linux中绘制2D图 [英] How to draw 2D diagram in linux
问题描述
我有一个.txt文件争用点,如下所示:
I have a .txt file contatning points which is look like following:
##x1 y1 x2 y2
123 567 798 900
788 900 87 89
....
我想绘制2D图,该图使用gnuplot在.txt文件的行中链接每一对.
I want to draw 2D diagram which link each pair in line of the .txt file using gnuplot.
我找到了这个在gnuplot中绘制点和线,但是我没有我不知道该如何使用gnuplot.
I find this Draw points and lines in gnuplot, but I don't know how to use gnuplot in my case.
图类型没有限制. 我在此处我可以使用以下命令将终端设置为png:
There is no limit for the diagram type. As I see here I could set terminal to png using following:
set terminal png
对于线条样式,我在此处:
set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1.5
并且不需要轴的标签.
推荐答案
已更新
根据您对行数和最大x,y尺寸的答复,此答案底部的基于ImageMagick的原始方法显然不适合您的特定问题.但是,我将其留给其他人查看,因为最多可以容纳几十行.我现在提供一个更合适的gnuplot
版本.
In the light of your reply about the number of lines and the maximum x,y dimensions, my original, ImageMagick-based approach at the bottom of this answer is clearly NOT the right one for your specific problem. However, I will leave it for others to see as it would be perfectly fine for up to a few dozen lines. I am now providing a more appropriate gnuplot
version.
Gnuplot版本
如果要使用gnuplot
进行操作,它将类似于以下内容:
If you want to do it with gnuplot
, it would look something like this:
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
的文件中,则可以使用
If you save that in a file calle plot.cmd
, you can then run it with
gnuplot < plot.cmd
如果要使用箭头,请使用以下变体:
If you want arrowheads, use a variant like this:
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
Magick ++和C ++答案
我决定得出Magick ++和C ++的答案,只是为了好玩.代码看起来像这样-顶部的注释中显示了要编译的命令.
I decided to work out a Magick++ and C++ answer, just for fun. The code looks like this - and the command to compile is shown in the comments at the top.
////////////////////////////////////////////////////////////////////////////////
// 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;
}
我用这样的Perl生成了1,000行随机测试数据:
I generated 1,000 lines of random test data with Perl like this:
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
结果如下:
原始答案
您还可以使用ImageMagick轻松地完成此操作,无论如何,它已经安装在大多数Linux发行版中.以下实际上只有4行代码-其余全部为注释:
You could also do it quite easily with ImageMagick which is already installed on most Linux distros anyway. There are actually only 4 lines of code in the following - the rest is all comments:
#!/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
输出
Read line 123,567 798,900
Read line 788,900 87,89
这篇关于如何在Linux中绘制2D图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!