我想寫一個小 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 終端中使用本手冊,它會繪制想要的資料。問題是它沒有忽略“標題”(國家、專案型別、訂單日期、總利潤)
uj5u.com熱心網友回復:
我不清楚您是在尋找 bash 腳本還是 gnuplot 腳本,可能兩者兼而有之。我的理解是你想從命令列呼叫來生成一些圖。因此,您需要從 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':)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417025.html
標籤:
