Plotting smooth sphere from discrete data with gnuplot - gnuplot

I have data generated from python code as such:
u, v = np.mgrid[0:2*np.pi:180*1j, 0:np.pi:90*1j]
X = np.cos(u)*np.sin(v)
Y = np.sin(u)*np.sin(v)
Z = np.cos(v)
This is written to a file tmp.dat, and I attempt to plot it in gnuplot with:
set pm3d
set palette
set hidden
splot "tmp.dat" using 1:2:3 with pm3d
However, this gives me:
It's in the general vicinity of what I want, but I'd like a smooth sphere as opposed to this. (My real data is in the same vein; with an enclosed surface I want to have transparency on.) I've tried adding set dgrid3d 50,50 to try and interpolate, however, I don't actually understand what I'm getting from this:
Any help or advice would be enormously appreciated. Changing the data to parametric (with u and v sweep data) is an option, however, I'm not certain how to do that - when I tried, the result was the same.

Not a complete answer, but part of your problem is that "set hidden3d" is not usable here. Gnuplot has two separate subsystems for representing surfaces. One of them, controlled by "set hidden3d", tracks bounding line segments and can remove occluded portions. The other is pm3d, which uses solid-fill quadrangles rather than bounding line segments. pm3d plots do not have the option of removing occluded quadrangles, but you can get a similar effect by depth-sorting them. The relevant command is
set pm3d depthorder
This works reasonably well if the individual quadrangles are approximately square but it gives bad results for long thin quadrangles, since the two ends of the facet can have conflicting depths with regard to neighboring facets.

Related

Trying to plot contours on Mac using the same script I use on windows on gnuplot

I have an issue with gnuplot on Mac. On windows everything seems fine I can easily draw contours on a "heat map". But on my Mac using the same script I can plot the map but not the contours on top of it.
https://imgur.com/a/x00bFnJ here is the two images. One with the contours is on Windows, other one is on Mac.
https://justpaste.it/6ws4h and here is the script.
I tried grid3d but they seem like not nearly accurate as the ones I have plotted on windows.
I cannot explain the difference between your two machines, but I think I can see where the problem arises. The following lines from your script are inconsistent with producing a plot with both a surface and contours shown:
set style data pm3d
set style function pm3d
set pm3d explicit at b
splot DATA with pm3d # command simplified
The issue is that both the default style and the explicit plot command specify pm3d, which is a filled-area surface plot. But contours need to be drawn with lines or with labels. You need some combination of defaults and explicit styles that allows you to mix both a surface and lines. I think either of these would do the trick:
set pm3d explicit
set style data lines
splot DATA with pm3d
or
set pm3d implicit
splot DATA with lines
Well! I will answer my own question.
set pm3d map
set contour surface
set cntrparam ...
sp "DATA" u 1:2:3 with pm3d lw 2,"" u 1:2:3 with lines nosurf
Solved my problem.!

artifacts in transparent fill when plotting data files with gnuplot [duplicate]

I have noticed that Gnuplot produces ugly artefacts when dealing to filled elements.
One instance is in the palette of the next figure:
Another example is when using filledcurves between two curves defined from points in ASCII files. In this case, you can see that rather than a real solid fill between the lines, the area is filled with number of strips, that only become apparent after zooming quite a bit, but that has very strong impact when rastering the image to png or similar:
This seems to be independent on the terminal. I have tried postscrip, pdfcairo and even tikz. Is there anything that can be done to improve this, or is this a hard limitation of Gnuplot?
Unfortunately, this is an artifact due to antialiasing in the document viewer when you have two filled polygons touching each other. This happens with the filledcurves plotting style, which composes the filled area of many quadrangles, as well as with the pm3d style (as you can see in the colorbox, which shows the same artifacts). See also
problematic Moire pattern in image produced with gnuplot pm3d and pdf output. for a concrete demo case.
There is a workaround, which however is very cumbersome. You must generate a filled polygon object with some script, fill that, use stats to determine the ranges, plot an empty plot (see e.g. Gnuplot - how can I get a figure with no point on it ? (I want to have only the axes, the title and the x- and y- labels)).
I assume, that you have a data file with three columns, and you would plot them with
plot 'test.dat' using 1:2:3 with filledcurves
Using the following very crude python script
from __future__ import print_function
from numpy import loadtxt
import sys
M = loadtxt(sys.argv[1])
print('set object 1 polygon ', end='')
for i in range(0,len(M)):
if (i == 0):
print('from {0},{1} '.format(M[i][0], M[i][1]), end='')
else:
print('to {0},{1} '.format(M[i][0], M[i][1]), end='')
for i in range(len(M)-1,-1,-1):
print('to {0},{1} '.format(M[i][0], M[i][2]), end='')
You can plot the filled curve with
# determine the autoscaling ranges
set terminal push
set terminal unknown
plot 'test.dat' using 1:2, '' using 1:3
set terminal pop
set xrange [GPVAL_X_MIN:GPVAL_X_MAX]
set yrange [GPVAL_Y_MIN:GPVAL_Y_MAX]
eval(system('python script.py test.dat'))
set object 1 polygon fillstyle solid noborder fillcolor rgb 'red'
plot NaN notitle
That, doesn't yet cover the problem with the jagged colorbox :(

Improving gnuplot 3d plots from data files with pm3d

I'm trying to graph an approximate solution (finite element method) to the Navier-Stokes equation. I've got a file called plotNSu1 that looks like this:
6 1 4.21022e-34
5.95 1 8.15227e-34
5.9693 0.970854 0.055197
6 1 4.21022e-34
5.9693 0.970854 0.055197
6 0.95 0.0941333
6 1 4.21022e-34
5.9693 0.970854 0.055197
5.92625 0.951192 0.0915468
5.9693 0.970854 0.055197
5.95 1 8.15227e-34
5.92625 0.951192 0.0915468
...
(The entries are x y z, grouped so that there's a blank line between each set of 4 points).
I use the following command to plot:
splot "plotNSu1" with lines
Butt I would like to make it look nicer, easier to read. (There's a small dip at one spot that's difficult to see, but is an important part of the solution).
I've found lots of examples using pm3d interpolate, but none seem to work for me. I've tried set style pm3d, and other set style commands, I've tried set pm3d map interpolate 10,10, and splot "plotNSu1" with pm3d, as well as so many other similar things stolen from the examples I've found that I can't keep track. Most of the time I just get an empty window with the color key and no plot, and often it sends me an empty 2d window.
I would like to have the color change with the contour of the plot, to make the little dip easier to see. Does anyone know how to do this? Here's one of the websites I've been trying to use, to see what I'm aiming for: http://gnuplot.sourceforge.net/demo/pm3d.html
I tried to post the image, but as I do not have enough reputation, I can't. But it's difficult to see what's going on because the graph is just all red.
I found a way to make this work, more or less, thanks to #Christoph's help and this other question suggested by stackoverflow: gnuplot pm3d plot triangle data
With my data in the file described above, in gnuplot I type:
set dgrid3d
set pm3d corners2color c2
splot "plotNSu1" using 1:2:3 with pm3d
The result isn't perfect, it looks a little choppy, but it basically does what I wanted, I can see the "dip" in my graph much better now.

gnuplot - How to extract pm3d interpolated data?

I am trying to smooth some sparse data I have (format x,y,z) in a gnuplot graph. I need to plot using plot and the with image option because files generated with pm3d map are exceedingly oversized. What I like about pm3d map is that I can smooth my data with interpolate in such a way that the interpolated point only considers neighboring points (dgrid3d considers every point in the graph for the smoothing).
Hence what I want to do is set table and then export the pm3d map data with the interpolation set to whatever I desire, then use that data to do a plot ... with image. The problem is that when I do the following:
set table "out.dat"
set pm3d map interpolate 10,10
splot "in.dat"
gnuplot ignores the interpolate option and simply writes to "out.dat" the same info that was written in "in.dat".
Any ideas?
Alternatively, any stand alone code that interpolates 3D data in the same fashion of pm3d would also be useful.
Edit:
Apparently, I am using at work an "oldish" version of gnuplot: gnuplot 4.2 patchlevel 6, which only accepts the dgrid3d behavior as described above. At home, I am using a newer version (gnuplot 4.4 patchlevel 3) which comes with the option set dgrid3d {<rows>{,<cols>}} splines. This does more or less what I want.

gnuplot ignores x and y ranges when using dgrid3d

I have a file with scattered data (points located approximatelly on the vertices of a regular grid): first two columns are the x and y coordinates, then a few more columns with other data that I need to plot. I want to obtain color maps that represent this data, and since points are scattered I'm using dgrid3d to generate a regular grid and have a smoother representation. My problem is that when I set dgrid3d, gnuplot ignores the x and y ranges and plot the grid outside the figure frame. Bellow is a minimal script to reproduce my problem:
set view map
set yrange [0.4:0.8]
set xrange [0.2:0.8]
set pm3d
set style data lines
set dgrid3d 100,100,4
splot "./Terr.dat" using 1:2:(log($6)) pal
The result that I obtain is the following image:
Setting the option clip1in or clip4in of pm3d has no effect. If I unset view so that the result is a 3D surface, it also ignores the x and y ranges. I could easily write an script to pre-process the data and remove the points outside the range I want, but gnuplot should be able to manage this. Any idea?
I'm using gnuplot 4.2 patchlevel 6
Thanks!
I'm not sure that I am able to reproduce your problem, but there are a few funny things with your script. I'm not exactly sure what the line set style data lines is supposed to do in this context as you're plotting with pm3d. I created a simple datafile:
0 1 4
1 0 5
0 0 2
1 1 3
And I plotted it using this script:
set view map
set yrange [0.4:0.8]
set xrange [0.2:0.8]
set dgrid3d 100,100,4
splot 'test.dat' u 1:2:3 w pm3d
And it seemed to "work" (I'm using gnuplot 4.6.0).
There are a few things of note however -- Notice that every point in my original domain was out of the given x and y ranges. Gnuplot still used those points when constructing the surface. This is also demonstrates reasonably nicely what the gnuplot weighting function looks like (although we could do even better by using only 1 point in our data file.)
UPDATE
Between my 2 computers, I have access to gnuplot4.2.6, gnuplot4.3.0, gnuplot4.4.2, gnuplot4.6.0, gnuplot4.6.1 and gnuplot4.7.0. gnuplot4.2.6 is the only version which exhibits the behavior you describe. It looks to me like they changed the behavior of pm3d in the 4.3 CVS branch, but didn't push those changes back into gnuplot4.2. The easy fix is to upgrade to gnuplot4.6 -- I've been using it as my default gnuplot for a few months now and it seems to be pretty stable.

Resources