
我有 6 個檔案需要繪製為帶有誤差範圍的折線圖並將它們輸出到不同的 png 檔案。文件格式如下。
秒 平均值 最小值 最大值
我將如何自動繪製這些圖表?所以我運行一個名為 bash.sh 的文件,它將獲取 6 個文件並將圖表輸出到不同的.png
文件。還需要標題和軸標籤。
答案1
如果我理解正確的話,這就是你想要的:
for FILE in *; do
gnuplot <<- EOF
set xlabel "Label"
set ylabel "Label2"
set title "Graph title"
set term png
set output "${FILE}.png"
plot "${FILE}" using 1:2:3:4 with errorbars
EOF
done
這假設您的檔案全部位於目前目錄中。上面是一個將產生圖表的 bash 腳本。就我個人而言,我通常使用某種形式的腳本編寫一個 gnuplot 命令文件(稱之為gnuplot_in
),為每個文件使用上述命令並使用gnuplot < gnuplot_in
.
給你一個例子,在Python中:
#!/usr/bin/env python3
import glob
commands=open("gnuplot_in", 'w')
print("""set xlabel "Label"
set ylabel "Label2"
set term png""", file=commands)
for datafile in glob.iglob("Your_file_glob_pattern"):
# Here, you can tweak the output png file name.
print('set output "{output}.png"'.format( output=datafile ), file=commands )
print('plot "{file_name}" using 1:2:3:4 with errorbars title "Graph title"'.format( file_name = datafile ), file=commands)
commands.close()
其中Your_file_glob_pattern
是描述資料檔案命名的內容,可以是*
或*dat
.當然glob
,您也可以使用模組來取代模組。os
確實,無論什麼都會產生檔案名稱清單。
答案2
Bash 解決方案,使用臨時命令檔:
echo > gnuplot.in
for FILE in *; do
echo "set xlabel \"Label\"" >> gnuplot.in
echo "set ylabel \"Label2\"" >> gnuplot.in
echo "set term png" >> gnuplot.in
echo "set output \"${FILE}.png\" >> gnuplot.in
echo "plot \"${FILE}\" using 1:2:3:4 with errorbars title \"Graph title\"" >> gnuplot.in
done
gnuplot gnuplot.in
答案3
這可能會有所幫助。
#set terminal postfile (These commented lines would be used to )
#set output "d1_plot.ps" (generate a postscript file. )
set title "Energy vs. Time for Sample Data"
set xlabel "Time"
set ylabel "Energy"
plot "d1.dat" with lines
pause -1 "Hit any key to continue"
將腳本檔案執行為gnuplot filename
.