2013-08-21 128 views
4

相關的how to set label and line the same color in gnuplot標籤風格 - 但不完全一樣的...使用此代碼:設置可變文本顏色和色點在gnuplot的

set style line 1 linetype 1 linewidth 2 pointtype 3 linecolor rgb "aquamarine" 
set style line 2 linetype 1 linewidth 2 pointtype 3 linecolor rgb "blue" 
set style line 3 linetype 1 linewidth 2 pointtype 3 linecolor rgb "coral" 

set xrange [0:3] 
set yrange [0:3] 

# function to get style index for coloring: 
getCol(x) = (x==0)?1:((x==1)?2:3); 

plot \ 
    '-' using ($1+0.5):($2+0.5):(getCol($2)) with impulses \ 
    lc variable notitle, \ 
    "" using ($1+0.5):($2+0.5):(stringcolumn(2)):(getCol($2)) with labels \ 
    textcolor variable point linestyle 1 pointtype 7 lc variable \ 
    font ",6" offset character 1.0,character 1.0 notitle 
0 0 
1 1 
1.5 1 
2 2 
e 
0 0 
1 1 
1.5 1 
2 2 
e 

...我得到這樣的輸出:

gnuplot-out

對於impulses而言,它看起來像一切都很好 - 但對於labels,該方法似乎有效 - 但它好像從不同索引中讀取顏色一樣!

那麼,如何使用相同的函數 - 如脈衝線,如何獲得點和標籤具有相同的變量顏色?這是在gnuplot 4.6 patchlevel 1 ...

+0

這個問題已在新發布的4.6 patchlevel 4中修復。只需在繪圖之前添加'set style increment user'行,就可以得到預期的結果(與[我的回答](http:// stackoverflow.com/a/18368864/2604213))。 – Christoph

回答

3

impulses的顏色索引用於選擇線條樣式,而對於labels則使用線條類型。這種行爲在當前的開發版本中仍然存在。根據文件,labels的行爲是正確的。 help linecolor variable說:「lc variable告訴程序使用從輸入數據的一列中讀取的值作爲線型索引...使用​​」可以類似地設置文本顏色「。錯誤,功能或錯誤的文檔?

作爲一種變通方法,您可以使用lc palette和適當定義palette

set style line 1 linetype 1 linewidth 2 pointtype 3 linecolor rgb "aquamarine" 
set style line 2 linetype 1 linewidth 2 pointtype 3 linecolor rgb "blue" 
set style line 3 linetype 1 linewidth 2 pointtype 3 linecolor rgb "coral" 

set palette defined (1 "aquamarine", 2 "blue", 3 "coral") 
unset colorbox 
set xrange [0:3] 
set yrange [0:3] 

# function to get style index for coloring: 
getCol(x) = (x==0)?1:((x==1)?2:3) 

unset key 
plot \ 
    '-' using ($1+0.5):($2+0.5):(getCol($2)) with impulses lc variable, \ 
    '' using ($1+0.5):($2+0.5):(stringcolumn(2)):(getCol($2)) with labels \ 
    tc palette point ls 1 pt 7 lc palette offset 1,1 
0 0 
1 1 
1.5 1 
2 2 
e 
0 0 
1 1 
1.5 1 
2 2 
e 

這給出結果: enter image description here

我希望變通辦法還你的真實情況。如果由於某種原因,你不能重新定義調色板,您可以用lc rgb variable,在這種情況下,最後一列必須是相應的RGB顏色的整數表示:

set style line 1 linetype 1 linewidth 2 pointtype 3 linecolor rgb "aquamarine" 
set style line 2 linetype 1 linewidth 2 pointtype 3 linecolor rgb "blue" 
set style line 3 linetype 1 linewidth 2 pointtype 3 linecolor rgb "coral" 

set xrange [0:3] 
set yrange [0:3] 

rgb(r,g,b) = 65536 * int(r) + 256 * int(g) + int(b) 
rgb1 = rgb(2, 215, 245) # something like aquamarine 
rgb2 = rgb(0, 0, 255) 
rgb3 = rgb(245,140,2) 
getCol(x) = (x==0)?rgb1:((x==1)?rgb2:rgb3) 

unset key 
plot \ 
    '-' using ($1+0.5):($2+0.5):(getCol($2)) with impulses \ 
    lc rgb variable, \ 
    '' using ($1+0.5):($2+0.5):(stringcolumn(2)):(getCol($2)) with labels \ 
    tc rgb variable point ls 1 pt 7 lc rgb variable offset 1,1 
0 0 
1 1 
1.5 1 
2 2 
e 
0 0 
1 1 
1.5 1 
2 2 
e 

您可能只需要tweek的RGB值。

但是,我會在gnuplot郵件列表中提出這個討論,看看這是否真的是一個錯誤,或者其他什麼。

+0

非常感謝@Christoph - 偉大的答案;無意中,我發現了這個相同的'set palette' /'unset colorbox'解決方法,所以它也適用於我的測試用例。但瞭解'lc rgb變量'方法是件好事。再次感謝 - 歡呼! – sdaau

+0

@sdaau需要一些時間來發現'gnuplot'提供的所有不同的可能性,以做同樣的事情':)'很高興聽到它幫助你。 – Christoph