I am try to plot a graph in GNUPLOT which has both positive/negative values. It is plotting in 1st quadrant.I mean x axis start from some negative value, same with y axis. I want to plot on both sides, like we see a 4 quadrants. Like in middle should be 0, on left negative values and on right positive value, for both x and y axis. Please help.
Thanks in advance.
Please make sure that you've done a reasonable search before asking. There are a lot of examples around. Although, sometimes it might not be easy to find the exact graph configuration you are looking for. Basically, everything is in the manual, although it can be confusing for a gnuplot beginner.
You can get help for almost every keyword via typing help <keyword> in the gnuplot console, e.g. help xtics, help zeroaxis, help border, etc.
If I understood your question correctly, you are probably looking for something like this:
Code:
### plot a function in a 4-quadrant graph
reset session
set size square
set xrange[-10:10]
set yrange[-10:10]
set border 0
set xzeroaxis lt -1
set yzeroaxis lt -1
set xtics axis nomirror
set ytics axis nomirror
set mxtics 5
set mytics 5
set grid x y mx my
set xtic add ('' 0) # remove 0 from xtics
set ytic add ('' 0) # remove 0 from ytics
set key top left
f(x) = 0.02*x**3 - 0.5*x
plot f(x) w l lc "red"
### end of code
Result:
Related
I would like to create a color bar plot of a single variable that draws a box to the left in red if the variable is negative and green to the right if positive.
I am failing to get rid of the y axis completely. There shall be no marking of it whatsoever. Second the x-axis and tics is hidden behind the box. I need it visible. Third the plot of the line at 0 is really unnecessary as I have already drawn all I need but gnuplot wants a plot cmd with some sort of argument. I tried plot 0 lt bgnd but that left an ugly white line in my box. I guess I can live with that. Arrows at the ends of the x-axis would be nice, too.
This is the current state of the code. (the variable v will later come from the outside world as command line argument)
v= 7.3
if (v<0){boxcolor= 'red'}
if (v>=0){boxcolor= 'green'}
unset border
unset ytics
unset key
set yzeroaxis
set xzeroaxis
set xtics axis
unset ytics
set xrange [-10:10]
set object 1 rect from 0.0,-0.5 to v,0.5 back fillcolor rgb boxcolor
plot 0
Result currently:
You are probably looking for something like this:
Update: improved version
using graph and first coordinates for the arrow (check help coordinates), hence independent of the actual x-range.
using xzeroaxis (check help xzeroaxis)
Script:
### only x-axis with arrows
reset session
set border 0
unset ytics
v= 7.3
boxcolor = (v<0) ? 'red' : 'green'
set xrange [-10:10]
set xtics axis mirror
set xzeroaxis lt 1 lc "black"
set object 1 rect from 0.0,-0.5 to v,0.5 behind fillcolor rgb boxcolor
set arrow 1 from graph -0.03, first 0 to graph 1.03, first 0 heads filled
plot cos(x)
### end of script
Result:
Another answer: This one uses the built-in axis variants rather than an arrow:
set border 0
unset key
# In newer gnuplot versions there is a keyword "nodraw"
# Here we define a synonym that works with older versions also
hide = -4
set yzeroaxis lt hide lc hide
set xzeroaxis lt black
set tics front
unset ytics
set xtics axis
# define rectangle here
set object 1 rect from 0,-.5 to 5,.5 behind fs noborder fc "green"
#
plot 0 with lines lc "black"
I plotted a figure with a classical x-axis, let's say from 0 to 25.
I'd like to have the x2-axis having the sqrt of x1-values and I didn't find anything about how to do it.
With my values here, x2range would be [0:5]
But tics shouldn't be linear, tha's why i didn't succeed to make it.
x2-tic 5 would match with 25, x2-tic 4 with 16, 3 with 9, etc.
Any Help ?
I hope there's a not-too-hard way to do this
Cheers.
That can be done since gnuplot 5 with the command set link, which links a secondary axis to the respective primary axis via a transformation function:
set xrange [0:25]
set link x2 via sqrt(x) inverse x**2
set x2tics
set xtics nomirror
plot x
The commands as given above will generate a warning because the mapping isn't unique. But you can ignore that warning as long as your xrange starts at value greater or equal to zero.
For gnuplot 4.6 you must place all x2tics manually:
set xrange [0:25]
set x2range [0:25]
set xtics nomirror
# delete all automatic tics but the one at 0
set x2tics (0)
# add all other tics
set for [i=1:5] x2tics add (sprintf('%d', i) i**2)
plot x
I have something like:
set grid xtics lt 0 lw 1 lc rgb "#a9a9a9"
set grid ytics lt 0 lw 1 lc rgb "#a9a9a9"
or the same without the "xtics" tag, and that works fine!
But if i add:
unset xtics
Then the grid disappears too :(
How can I only have a grid, without tics?
To hide the major ticks, you can use set tics scale 0:
set grid
set tics scale 0
plot x
Note, if one day you also want to use minor ticks and also hide them, you must use set tics scale 0,0.001.
If you only want to make the tic labels disappear then use set format:
set format x ""
set format y ""
set grid
plot x
If you don't want the tics either, then as far as I know you'd need a more complicated script, possibly using iterators and arrows, for example the following (you'll have to change the limits depending on your xrange and yrange and the arrow style to your liking):
unset xtics
unset ytics
set for [i=-5:5:5] arrow from i,-10 to i,10 nohead
set for [j=-5:5:5] arrow from -10,j to 10,j nohead
plot x
I'm trying to plot data with two different x-axes with the following code:
reset
# Settings
set grid
set x2tics
set xtics nomirror
plot './data/N.dat' u ($1*c1)/f_offset:($2*c2) w lp ls 1,\
'' u ($1*c1)/f_offset:($3*c2) w lp ls 2,\
'' u ($1*c1):($2*c2) w p ps 0 notitle axis x2y1
where c1,c2 and f_offset are constants. The last line is there only to get the second set of ticks. This works fine but the ticks on x and x2 axes are not aligned. It is a minor thing but it does look weird with the grid, which is aligned to the first axis. Is there a simple solution I'm missing or do I have to set the values manually?
Actually, there is a simple solution, although there might be something similar as an option I'm not aware of. Simply setting the ticks manually works in this case:
set x2tics 0.9*f_offset,0.005*f_offset,0.935*f_offset
set xtics nomirror 0.9,0.005,0.935
I don't know why I didn't try that earlier.
I'm making some 3D surface plots in Gnuplot and it would be very useful to have tic marks along each border of my plot. In the attached sample plot, there are no tic marks along the top left or top right horizontal borders (borders 256 and 512). In order for vertical grid lines to be drawn on the back vertical planes, I need to have tic marks on these borders. How can I achieve this?
I have not found a way to solve this using the grid and border. However, there is a relatively simple workaround, which is useful only if you do not change your ranges every time you plot your data.
Basically you plot a constant surfaces on the back walls matching the line type and number and position of the grid lines in the x-y plane.
First, set up the ranges. I labeled them, because we will need the numbers later.
xmin=0 ; xmax = 100
ymin=0.01 ; ymax=1000
zmin=0 ; zmax=990
set xrange [xmin:xmax]
set yrange [ymin:ymax]
set zrange [zmin:zmax]
Setting the z axis intersection with x-y plane (ticslevel) and I guessed a view angle to visually match your example. We want to set these before multiplot.
set ticslevel 0.0
set view 60,45,1
Now comes the fun. For this part you have to know the number of grid lines in your x-y plane (same as number of major tics on x and y axes). We will plot the back grid walls first, so they are behind your data/function at the end. Also, I switch off the grid and border for this part, but they should exactly overlap if you leave them in.
set multiplot
unset grid
set border 0
Even though the y-axis has logarithmic scale, the grid is separated linearly (equidistant grid lines). So at this point I want linear scale on y axis. (If you set logscale y before this point, comment it out.) I don't want linear labels on the y-axis, because they are different on logarithmic scale, so I set the format accordingly.
set format y ""
set isosamples 6,9 # - set this to number of tics on y-axis,z-axis
Here is the only manual setting that might change when you plot on a different range. You need to set the numbers of isosamples to the numbers of tics on y-axis,z-axis.
To control how many lines gnuplot is going to use for each surface, we need to set both the isosamples (done above) and the ranges of dummy variables u,v. Notice the line type 0, which is the grid line type.
I plot my first wall at x = xmin:
set parametric
set urange [ymax:ymin]
set vrange [zmax:zmin]
splot xmin,u,v w lines lt 0
Similarly, we do the other wall at y = ymax.
set urange [xmin:xmax]
set vrange [zmin:zmax]
splot u,ymax,v w lines lt 0
unset parametric
Now that I have the walls, I can plot what you already have in the picture. Setting borders, tics, re-enable the y-axis label we disabled before, set the log-scale on y axis (now is a good time) and reset isosamples to the default values.
set xtics mirror
set ytics mirror
set ztics mirror
set grid ytics xtics back
set logscale y
set format y " %g"
set isosamples 10,10
And plot your data/function as you are used to.
splot 'data.txt' w lines
unset multiplot
And we are done....
Possible necessary modification:
I guess your x and y axes will be different from mine, since the reverse is easily achievable by something like view 60,135,1. This also switches x and y though. Your surfaces will then change coordinates.
You can try setting tics there using x2tics and y2tics.
Here is the documentation of Xtics.