Могу ли я разместить сетку gnuplot иначе, чем тики? - PullRequest
1 голос
/ 30 сентября 2019

Как я могу добавить X-сетку с разным интервалом, чем тики? Мой график представляет собой гистограмму, показывающую количество патентов (определенного типа), выданных за год, и диапазон лет велик (1807-1971). Я хотел бы отмечать / маркировать каждый год, но добавляю только X линий сетки каждое десятилетие (а также использую разные цвета для соответствующих десятилетних меток).

Я искал ответ и пробовал что-то длячасов и некуда. Возможны ли эти варианты?

Мой текущий график (без сетки X) выглядит следующим образом:

gnuplot histogram

И сценарий такой:

set style data histograms
set style histogram gap 1
set style fill solid
set title "Number of Prism Glass Patents Granted" font "fixed, 24" offset 0,-0.9
set xlabel "Year" font "fixed,18" offset 0,0.8
set nokey
set xtics out nomirror rotate font "fixed, 8" offset 0,0.4
set grid y
plot 'frequency.dat' using 2:xtic(1) linecolor 'blue'

Ответы [ 3 ]

2 голосов
/ 30 сентября 2019

Сетка для каждой оси создается из тиков для этой же оси, так что да, они всегда совпадают. Однако, если ваш график использует только ось x1, вы можете определить диапазон и метки для оси x2 и включить сетку только для x2, а не для x1.

В последних версиях gnuplot есть команда set link x2это гарантирует, что оси x1 и x2 согласовывают диапазон и масштаб. Если ваша версия не поддерживает это, вы все равно можете установить их для явного соответствия:

set xrange [min:max]
set x2range [min:max]

set xtics <whatever>        # these will label the actual plot
set x2tics <something else> # these will be used only for grid lines
set x2tics scale 0.0 format ""  # show no x2 tics or labels on the plot

set grid x2 nox
plot ...
2 голосов
/ 30 сентября 2019

Я предполагаю, что ваши данные состоят из двух столбцов: год и количество патентов. Почему вы используете xitc(1), это необходимо для маркировки каждый год? Как насчет использования младшего и основного XTIC? Я бы использовал plotstyle with boxes.

Код:

### major and minor xtics
reset session

# generate some random data
set print $Data
    do for [i=1807:1971] {
        print sprintf("%d %d", i, int(rand(0)*100))
    }
set print

set xlabel "Year"
set xtics out nomirror
unset x2tics
set xtics 10
set mxtics 10
set grid ytics
set grid xtics
set boxwidth 0.5
plot $Data u 1:2 with boxes fill solid 1.0 lc rgb "blue" notitle
### end of code

Результат:

enter image description here

Дополнение:

Другая версия с сеткой каждые 10 лет и этикеткой другого цвета. Метки отображаются только при количестве патентов> 0. Вместо использования xtics это делается путем построения with labels.

Код:

### major and minor xtics
reset session

set term pngcairo size 1600,360
set output "tbGrid.png"

xmin = 1807
xmax = 1971

# generate some random data
set print $Data
    do for [i=xmin:xmax] {
        print sprintf("%d %d", i, int(rand(0)+0.4)*(int(rand(0)*100)))
    }
set print

set xlabel "Year" offset 0,-1.5
set xrange[xmin-1:xmax+1]
set xtics 10 format "" out nomirror

set mxtics 10
set bmargin 5
set grid ytics 
set grid xtics
set boxwidth 0.5

myTic(n,p) = p==0 ? "" : sprintf("%d",n)
myColor(n) = int(n)%10==0 ? 0xff0000 : 0x000000

plot $Data u 1:2 with boxes fill solid 1.0 lc rgb "blue" notitle, \
    '' u 1:(0):(myTic($1,$2)):(myColor($1)) with labels \
    tc rgb var rotate offset 0,-1.5 font ",8" notitle
set output
### end of code

Результат:

enter image description here

0 голосов
/ 02 октября 2019

Спасибо вам обоим - вот мой последний сюжет:

enter image description here

... и его сценарий:

# histogram of # of prism glass patents granted per year
###
set term png size 1800,600

xmin = 1807
xmax = 1971

set title sprintf("Prism Glass Patents Granted %d-%d", xmin, xmax) \
    font "fixed, 24" offset 0,-0.5

set xlabel "Year" font "fixed,24" offset 0,-2

# x tic for each year
set xrange [xmin-1:xmax+1]
set xtics 1 out nomirror format ""

# x2 tic and gridline for each decade
set x2tics 1800,10,1970
set x2tics out font "fixed, 12" offset 0,-0.6
set grid x2 nox

set grid y              # y axis is count

set boxwidth 0.5
set bmargin 5
set nokey

myTic(y,n) = n==0 ? "" : sprintf("%d",y)   # only label year if count>0
myColor(y) = y==1897 ? 0x0000FF : 0x000000 # highlight 1897 (biggest year)

plot 'frequency.dat' using 1:2 with boxes fill solid lc rgb "blue", \
        '' using 1:(0):(myTic($1,$2)):(myColor($1)) with labels \
        tc rgb var rotate offset 0.1,-1.2 font "fixed,8"
...