在.csv文件上使用Gnuplot的Bashscript

z4bn682m  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(171)

我想写一个小bash脚本,它只要求用户输入一个国家和一个项目。脚本将打开gnuplot,并从.csv和.png安全文件中绘制一些数据(国家和项目的订单日期和总利润)
以下是前几行(总共100k):

Country,Item Type,Order Date,Total Profit
Afghanistan,Baby Food,1/10/2010,672649.62
Afghanistan,Baby Food,1/23/2010,833406.84
Afghanistan,Baby Food,10/14/2011,893894.50
Afghanistan,Baby Food,10/20/2013,38919.16
Afghanistan,Baby Food,11/10/2015,51189.24
Afghanistan,Baby Food,11/19/2014,471247.76

我在gnuplot终端输入:

set title 'sales'
set xlabel 'date'
set ylabel 'profit'
set datafile separator ','
set xdata time
set timefmt "%m/%d/%y"
plot 100k.csv u 3:4 w lines

如果我在Gnuplot终端中使用这本手册,它会绘制出想要的数据。问题是它没有忽略“标题”(国家、项目类型、订单日期、总利润)

bgibtngc

bgibtngc1#

我不太清楚你是在找bash脚本还是gnuplot-script,或者两者都找。我所理解的是,你想从命令行进行调用来生成一些图。所以,你需要从CSV文件中过滤数据,生成一个图并保存到磁盘。
根据以下建议,您可以通过以下方式从命令行调用gnuplot:

gnuplot -c "SO70774684.gp" "Afghanistan" "Baby Food"

gnuplot会为您完成所有工作,例如过滤和生成PNG图形。
我假设条目是按日期排序的,否则线图没有太大意义,或者数据需要首先排序。您没有指定太多细节,因此有很大的调整空间。

数据:'SO70774684.csv',添加了更多行。

Country,     Item Type, Order Date, Total Profit
Afghanistan, Baby Food,  1/10/2010, 672649.62
Belgium,     Baby Food,  1/10/2010, 1111
Afghanistan, Baby Food,  1/23/2010, 833406.84
Cyprus,      Baby Food,  1/23/2010, 2222
Afghanistan, Baby Food, 10/14/2011, 893894.50
Denmark,     Baby Food, 10/14/2011, 3333
Afghanistan, Baby Food, 10/20/2013, 38919.16
Estonia,     Baby Food, 10/14/2011, 4444
Afghanistan, Baby Food, 11/19/2014, 471247.76
France,      Baby Food, 10/14/2011, 5555
Afghanistan, Baby Food, 11/10/2015, 51189.24
Germany,     Baby Food, 10/14/2011, 6666
Belgium,     Baby Food,  1/10/2010, 1000
Cyprus,      Baby Food,  1/23/2010, 2000
Denmark,     Baby Food, 10/14/2011, 3000
Estonia,     Baby Food, 10/14/2011, 4000
France,      Baby Food, 10/14/2011, 5000
Germany,     Baby Food, 10/14/2011, 6000

代码:(gnuplot脚本文件'SO70774684.gp'

### create gnuplot graph from command line call
reset session

FILE    = 'SO70774684.csv'
Country = ARG1
Item    = ARG2

set datafile separator comma
set xdata time
set timefmt "%m/%d/%Y"

set term pngcairo size 640,384 font ",10"
set output sprintf("%s,%s.png",Country,Item)

set ylabel "Total profit" font ",11"
set format x "%m\n%Y"
set boxwidth 1.0 relative
set style fill solid 0.3
set key noautotitle
set grid x,y

set title sprintf("%s: %s", Country, Item)

myFilter(colD,colF1,F1,colF2,F2) = strcol(colF1) eq F1 && strcol(colF2) eq F2 ? column(colD) : NaN
set datafile missing NaN

plot FILE u 3:(myFilter(4,1,Country,2,Item)) w lp pt 7 lc "red"

set output
### end of code

结果:(文件:'Afghanistan,Baby Food.png'

相关问题