I am trying to create a 3D surface plot which looks like this:
Instead of plotting the equation, I am trying to plot my own set of data contained in the data.tsv file in the recommended grid format.
8417 5128 4661
7284 4940 3373
5220 3597 4088
For clarity, The XYZ representation of the above data is:
rec/s mb/s latency
640000 1024 5220
640000 2048 3597
640000 4096 4088
320000 1024 7284
320000 2048 4940
320000 4096 3373
160000 1024 8417
160000 2048 5128
160000 4096 4661
The gnuplot script that I am using to get the desired output is:
set term postscript eps enhanced color
set output '|ps2pdf - outputfile.pdf'
set bar 1.000000 front
set style circle radius graph 0.02, first 0.00000, 0.00000
set style ellipse size graph 0.05, 0.03, first 0.00000 angle 0 units xy
set style textbox transparent margins 1.0, 1.0 border
unset logscale
set samples 51, 51
set isosamples 21, 21
set style data lines
unset paxis 1 tics
unset paxis 2 tics
unset paxis 3 tics
unset paxis 4 tics
unset paxis 5 tics
unset paxis 6 tics
unset paxis 7 tics
set title "3D gnuplot demo"
set xlabel "X axis"
set xlabel offset character -3, -2, 0 font "" textcolor lt -1 norotate
set xrange [ 160000 : 640000 ] noreverse nowriteback
set ylabel "Y axis"
set ylabel offset character 3, -2, 0 font "" textcolor lt -1 rotate by -270
set yrange [ 1024 : 4096 ] noreverse nowriteback
set zlabel "Z axis"
set zlabel offset character -5, 0, 0 font "" textcolor lt -1 norotate
set paxis 1 range [ * : * ] noreverse nowriteback
set paxis 2 range [ * : * ] noreverse nowriteback
set paxis 3 range [ * : * ] noreverse nowriteback
set paxis 4 range [ * : * ] noreverse nowriteback
set paxis 5 range [ * : * ] noreverse nowriteback
set paxis 6 range [ * : * ] noreverse nowriteback
set paxis 7 range [ * : * ] noreverse nowriteback
set colorbox vertical origin screen 0.9, 0.2, 0 size screen 0.05, 0.6, 0 front noinvert bdefault
x = 0.0
# Last datafile plotted: "$grid"
splot 'data.tsv' using 1:2:3
However, the above script is giving me the following error:
"3d.gp", line 37: warning: No usable data in this plot to auto-scale axis range
splot 'data.tsv' using 1:2:3
^
"3d.gp", line 37: All points z value undefined
Format the data.tsv file like this :
640000 1024 5220
640000 2048 3597
640000 4096 4088
320000 1024 7284
320000 2048 4940
320000 4096 3373
160000 1024 8417
160000 2048 5128
160000 4096 4661
and plot the data with
splot 'data.tsv' using 1:2:3 w l
I did a sucessful test just with that last gnuplot command... if you want something more fancy, I can try to help
Related
I have data as shown below in a single column and i want to split that single column into n number of columns and name rows and columns. How can i do that in python ?
-----------sample data----------
5
3
5
0
0
1
0
0
18
23
11
1
2
10
1
0
5
6
1
0
1
1
1
0
158
132
150
17
------------ The output should look like ---------
column0 column1 column2 column3 column4 column5 column6
row1 5 0 18 2 5 1 158
row2 3 1 23 10 6 1 132
row3 5 0 11 1 1 1 150
row4 0 0 1 0 0 0 17
One of the easiest ways is to use numpy and the reshape function
import numpy as np
k = np.array(data)
k.reshape([row,column],order='F')
As for your example. You mentioned the data is from a text file so to acquire the data from the text file and reshape
import numpy as np
data = np.genfromtxt("sample-data.txt");
data.reshape([4,7],order='F')
output will be
Out[27]:
array([[ 5, 0, 18, 2, 5, 1, 158],
[ 3, 1, 23, 10, 6, 1, 132],
[ 5, 0, 11, 1, 1, 1, 150],
[ 0, 0, 1, 0, 0, 0, 17]])
I do not know the structure of the data but assuming it is in 1 giant column as seen in the sample above. When importing the data using open. The following happens.
data = open("sample-data.txt",'r').readlines()
data
Out[64]:
['5\n',
'3\n',
'5\n',
'0\n',
'0\n',
'1\n',
'0\n',
'0\n',
'18\n',
'23\n',
'11\n',
'1\n',
'2\n',
'10\n',
'1\n',
'0\n',
'5\n',
'6\n',
'1\n',
'0\n',
'1\n',
'1\n',
'1\n',
'0\n',
'158\n',
'132\n',
'150\n',
'17']
This results in an array of string values as the \n means next line. Assuming this is numerical data, you will want to use the code above to get the numbers.
I am trying to make a 3D, surfaced graph using gnuplot (in C++). This is code I currently have.
//gp << "set dgrid3d\n";
//gp << "set samples 10,10\n";
//gp << "set isosamples 10,10\n";
//gp << "set contour\n";
//gp << "set hidden3d\n";
//gp << "set surface\n";
//gp << "set pm3d\n";
gp << "splot 't.dat' u 1:4:5 w linespoints pointtype 7 pointsize 1.5, \
't.dat' u 2:4:5 w linespoints pointtype 9 pointsize 1.5, \
't.dat' u 3:4:5 w linespoints pointtype 4 pointsize 1.5\n";
As you can see I have tried a number of commands (currently commented) to achieve the goal. I cannot seem to find a suitable combination of commands or a single command which gives me a 3D graph with a surface like which I seek.
This is 't.dat' - the data that I am attempting to plot:
#timeTaken1 timeTaken2 timeTaken3 D E
1.2342423 1.33 2.442 1 0
1.234234 1.55 2.236 1 20
2.56465 1.56 3.39 1 40
2.464 1.234 3.224 1 60
2.2747 1.768 3.552 1 80
2.34774 1.876 3.574 1 100
3.34747 2.94 4.795 2 0
3.34747 2.66 5.776 2 20
3.3747 3.234 5.666 2 40
3.787 3.66 6.503 2 60
3.456 3.88 6.37 2 80
4.345 3.345 5.853 2 100
Does someone know what needs to be done to make this work? Is there something wrong with the structure of the data? Is there some command I haven't seen?
With splot you can only plot your data points (and connect them) as you can in 2D. To draw a surface, you have to find out an f(x,y) function and also splot it. Or you can manually interpolate a hundred or thousand of the surface coordinates into 't2.dat' and splot 't2.dat' w l.
I need to programatically get the refresh rate of a monitor.
When I type xrandr (1.4.1, opensuse 13) on the command line I get:
Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 16384 x 16384
VGA-0 disconnected primary (normal left inverted right x axis y axis)
DVI-D-0 connected 1920x1200+0+0 (normal left inverted right x axis y axis) 518mm x 324mm
1920x1200 60.0*+
1920x1080 60.0
1680x1050 60.0
1600x1200 60.0
1280x1024 60.0
1280x960 60.0
1024x768 60.0
800x600 60.3
640x480 59.9
HDMI-0 disconnected (normal left inverted right x axis y axis)
This result is confirmed by nvidia-settings -q RefreshRate, among other things.
But ...
when I run the following code (origin: https://github.com/raboof/xrandr/blob/master/xrandr.c), compiled with g++ 4.8.1 (with -lX11 -lXext -lXrandr) :
int nsize;
int nrate;
short *rates;
XRRScreenSize *sizes;
Display *dpy = XOpenDisplay(NULL);
Window root = DefaultRootWindow(dpy);
XRRScreenConfiguration *conf = XRRGetScreenInfo(dpy, root);
printf ("Current rate: %d\n",XRRConfigCurrentRate(conf));
sizes = XRRConfigSizes(conf, &nsize);
printf(" SZ: Pixels Refresh\n");
for (int i = 0; i < nsize; i++) {
printf("%-2d %5d x %-5d", i, sizes[i].width, sizes[i].height);
rates = XRRConfigRates(conf, i, &nrate);
if (nrate)
printf(" ");
for (int j = 0; j < nrate; j++)
printf("%-4d", rates[j]);
printf("\n");
}
XRRFreeScreenConfigInfo(conf);
I get:
Current rate: 50
SZ: Pixels Refresh
0 1920 x 1200 50
1 1920 x 1080 51
2 1680 x 1050 52
3 1600 x 1200 53
4 1280 x 1024 54
5 1280 x 960 55
6 1024 x 768 56
7 800 x 600 57
8 640 x 480 58
9 1440 x 900 59
10 1366 x 768 60
11 1280 x 800 61
12 1280 x 720 62
Why am I getting this result?
What I am doing wrong?
The software uses OpenGL with GLEW. can this have any influence?
We do call glXQueryDrawable(dpy, drawable, GLX_SWAP_INTERVAL_EXT, &val) but afterwards, and I do not think this should have any influence.
I found the answer:
If the XRandR sever supports version 1.2 of the protocol, then the appropriate functions need to be used (wich I plan to do by copying snippets of code from https://github.com/raboof/xrandr/blob/master/xrandr.c where has_1_2 is true).
My code in the question uses functions for the version 1.1 of the protocol, and therefore only the metamodes are returned.
As a simple check, I tried the following two commands:
xrandr --q1
xrandr --q12.
And indeed the 1st one gives me the same result I programatically get.
Credits go to http://www.ogre3d.org/forums/viewtopic.php?f=4&t=65010&start=200
I'd like to draw filled ellipse with python. This would be easy if I could use PIL oder some other libraries. The problem is I need the ellipse in a .dxf file format. Therefore I used the dxfwrite package. This allows me to draw an ellipse but I couldn't find a way to fill it with a solid color. The following code does draw an ellipse line, but does not fill it.
import dxfwrite
from dxfwrite import DXFEngine as dxf
name = 'ellipse.dxf'
dwg = dxf.drawing(name)
dwg.add(dxf.ellipse((0,0), 5., 10., segments=200))
dwg.save()
Does anybody of you guys know a solution?
The HATCH entity is not supported by dxfwrite, if you use ezdxf this is the solution:
import ezdxf
dwg = ezdxf.new('AC1015') # hatch requires the DXF R2000 (AC1015) format or later
msp = dwg.modelspace() # adding entities to the model space
# important: major axis >= minor axis (ratio <= 1.) else AutoCAD crashes
msp.add_ellipse((0, 0), major_axis=(0, 10), ratio=0.5)
hatch = msp.add_hatch(color=2)
with hatch.edit_boundary() as boundary: # edit boundary path (context manager)
edge_path = boundary.add_edge_path()
# an edge path can contain line, arc, ellipse or spline elements
edge_path.add_ellipse((0, 0), major_axis_vector=(0, 10), minor_axis_length=0.5)
# upcoming ezdxf 0.7.7:
# renamed major_axis_vector to major_axis
# renamed minor_axis_length to ratio
dwg.saveas("solid_hatch_ellipse.dxf")
You could fill an ellipse by using a solid hatch object:
For the above example, here is a snippet from the DXF file that contains the ellipse and the hatch:
AcDbEntity
8
0
100
AcDbEllipse
10
2472.192919
20
1311.37942
30
0.0
11
171.0698134145308
21
-27.61597470964863
31
0.0
210
0.0
220
0.0
230
1.0
40
0.2928953354556341
41
0.0
42
6.283185307179586
0
HATCH
5
5A
330
2
100
AcDbEntity
8
0
100
AcDbHatch
10
0.0
20
0.0
30
0.0
210
0.0
220
0.0
230
1.0
2
SOLID
70
1
71
1
91
1
92
5
93
1
72
3
10
2472.192919357234
20
1311.379420138197
11
171.0698134145308
21
-27.61597470964863
40
0.2928953354556341
50
0.0
51
360.0
73
1
97
1
330
59
75
1
76
1
47
0.794178
98
1
10
2428.34191358924
20
1317.777876434349
450
0
451
0
460
0.0
461
0.0
452
0
462
1.0
453
2
463
0.0
63
5
421
255
463
1.0
63
2
421
16776960
470
LINEAR
1001
GradientColor1ACI
1070
5
1001
GradientColor2ACI
1070
2
1001
ACAD
1010
0.0
1020
0.0
1030
0.0
There are a lot of DXF codes involved. This is the information Autodesk provide:
Hatch group codes
Group code
Description
100
Subclass marker (AcDbHatch)
10
Elevation point (in OCS)
DXF: X value = 0; APP: 3D point (X and Y always equal 0, Z represents the elevation)
20, 30
DXF: Y and Z values of elevation point (in OCS)
Y value = 0, Z represents the elevation
210
Extrusion direction (optional; default = 0, 0, 1)
DXF: X value; APP: 3D vector
220, 230
DXF: Y and Z values of extrusion direction
2
Hatch pattern name
70
Solid fill flag (solid fill = 1; pattern fill = 0); for MPolygon, the version of MPolygon
63
For MPolygon, pattern fill color as the ACI
71
Associativity flag (associative = 1; non-associative = 0); for MPolygon, solid-fill flag (has solid fill = 1; lacks solid fill = 0)
91
Number of boundary paths (loops)
varies
Boundary path data. Repeats number of times specified by code 91. See Boundary Path Data
75
Hatch style:
0 = Hatch “odd parity” area (Normal style)
1 = Hatch outermost area only (Outer style)
2 = Hatch through entire area (Ignore style)
76
Hatch pattern type:
0 = User-defined; 1 = Predefined; 2 = Custom
52
Hatch pattern angle (pattern fill only)
41
Hatch pattern scale or spacing (pattern fill only)
73
For MPolygon, boundary annotation flag (boundary is an annotated boundary = 1; boundary is not an annotated boundary = 0)
77
Hatch pattern double flag (pattern fill only):
0 = not double; 1 = double
78
Number of pattern definition lines
varies
Pattern line data. Repeats number of times specified by code 78. See Pattern Data
47
Pixel size used to determine the density to perform various intersection and ray casting operations in hatch pattern computation for associative hatches and hatches created with the Flood method of hatching
98
Number of seed points
11
For MPolygon, offset vector
99
For MPolygon, number of degenerate boundary paths (loops), where a degenerate boundary path is a border that is ignored by the hatch
10
Seed point (in OCS)
DXF: X value; APP: 2D point (multiple entries)
20
DXF: Y value of seed point (in OCS); (multiple entries)
450
Indicates solid hatch or gradient; if solid hatch, the values for the remaining codes are ignored but must be present. Optional; if code 450 is in the file, then the following codes must be in the file: 451, 452, 453, 460, 461, 462, and 470. If code 450 is not in the file, then the following codes must not be in the file: 451, 452, 453, 460, 461, 462, and 470
0 = Solid hatch
1 = Gradient
451
Zero is reserved for future use
452
Records how colors were defined and is used only by dialog code:
0 = Two-color gradient
1 = Single-color gradient
453
Number of colors:
0 = Solid hatch
2 = Gradient
460
Rotation angle in radians for gradients (default = 0, 0)
461
Gradient definition; corresponds to the Centered option on the Gradient Tab of the Boundary Hatch and Fill dialog box. Each gradient has two definitions, shifted and unshifted. A Shift value describes the blend of the two definitions that should be used. A value of 0.0 means only the unshifted version should be used, and a value of 1.0 means that only the shifted version should be used.
462
Color tint value used by dialog code (default = 0, 0; range is 0.0 to 1.0). The color tint value is a gradient color and controls the degree of tint in the dialog when the Hatch group code 452 is set to 1.
463
Reserved for future use:
0 = First value
1 = Second value
470
String (default = LINEAR)
I hope this may be of some use to you. I apologize if I missunderstood your issue.
At Wikipedia's Mandelbrot set page there are really beautiful generated images of the Mandelbrot set.
I also just implemented my own Mandelbrot algorithm. Given n is the number of iterations used to calculate each pixel, I color them pretty simple from black to green to white like that (with C++ and Qt 5.0):
QColor mapping(Qt::white);
if (n <= MAX_ITERATIONS){
double quotient = (double) n / (double) MAX_ITERATIONS;
double color = _clamp(0.f, 1.f, quotient);
if (quotient > 0.5) {
// Close to the mandelbrot set the color changes from green to white
mapping.setRgbF(color, 1.f, color);
}
else {
// Far away it changes from black to green
mapping.setRgbF(0.f, color, 0.f);
}
}
return mapping;
My result looks like that:
I like it pretty much already, but which color gradient is used for the images in Wikipedia? How to calculate that gradient with a given n of iterations?
(This question is not about smoothing.)
The gradient is probably from Ultra Fractal. It is defined by 5 control points:
Position = 0.0 Color = ( 0, 7, 100)
Position = 0.16 Color = ( 32, 107, 203)
Position = 0.42 Color = (237, 255, 255)
Position = 0.6425 Color = (255, 170, 0)
Position = 0.8575 Color = ( 0, 2, 0)
where Position is in range [0, 1) and Color is RGB in range [0, 255].
The catch is that the colors are not linearly interpolated. The interpolation of colors is likely cubic (or something similar). Following image shows the difference between linear and Monotone cubic interpolation:
As you can see the cubic interpolation results in smoother and "prettier" gradient. I used monotone cubic interpolation to avoid "overshooting" of the [0, 255] color range that can be caused by cubic interpolation. Monotone cubic ensures that interpolated values are always in the range of input points.
I use following code to compute the color based on iteration i:
double smoothed = Math.Log2(Math.Log2(re * re + im * im) / 2); // log_2(log_2(|p|))
int colorI = (int)(Math.Sqrt(i + 10 - smoothed) * gradient.Scale) % colors.Length;
Color color = colors[colorI];
where i is the diverged iteration number, re and im are diverged coordinates, gradient.Scale is 256, and the colors is and array with pre-computed gradient colors showed above. Its length is 2048 in this case.
Well, I did some reverse engineering on the colours used in wikipedia using the Photoshop eyedropper. There are 16 colours in this gradient:
R G B
66 30 15 # brown 3
25 7 26 # dark violett
9 1 47 # darkest blue
4 4 73 # blue 5
0 7 100 # blue 4
12 44 138 # blue 3
24 82 177 # blue 2
57 125 209 # blue 1
134 181 229 # blue 0
211 236 248 # lightest blue
241 233 191 # lightest yellow
248 201 95 # light yellow
255 170 0 # dirty yellow
204 128 0 # brown 0
153 87 0 # brown 1
106 52 3 # brown 2
Simply using a modulo and an QColor array allows me to iterate through all colours in the gradient:
if (n < MAX_ITERATIONS && n > 0) {
int i = n % 16;
QColor mapping[16];
mapping[0].setRgb(66, 30, 15);
mapping[1].setRgb(25, 7, 26);
mapping[2].setRgb(9, 1, 47);
mapping[3].setRgb(4, 4, 73);
mapping[4].setRgb(0, 7, 100);
mapping[5].setRgb(12, 44, 138);
mapping[6].setRgb(24, 82, 177);
mapping[7].setRgb(57, 125, 209);
mapping[8].setRgb(134, 181, 229);
mapping[9].setRgb(211, 236, 248);
mapping[10].setRgb(241, 233, 191);
mapping[11].setRgb(248, 201, 95);
mapping[12].setRgb(255, 170, 0);
mapping[13].setRgb(204, 128, 0);
mapping[14].setRgb(153, 87, 0);
mapping[15].setRgb(106, 52, 3);
return mapping[i];
}
else return Qt::black;
The result looks pretty much like what I was looking for:
:)
I believe they're the default colours in Ultra Fractal. The evaluation version comes with source for a lot of the parameters, and I think that includes that colour map (if you can't infer it from the screenshot on the front page) and possibly also the logic behind dynamically scaling that colour map appropriately for each scene.
This is an extension of NightElfik's great answer.
The python library Scipy has monotone cubic interpolation methods in version 1.5.2 with pchip_interpolate. I included the code I used to create my gradient below. I decided to include helper values less than 0 and larger than 1 to help the interpolation wrap from the end to the beginning (no sharp corners).
#set up the control points for your gradient
yR_observed = [0, 0,32,237, 255, 0, 0, 32]
yG_observed = [2, 7, 107, 255, 170, 2, 7, 107]
yB_observed = [0, 100, 203, 255, 0, 0, 100, 203]
x_observed = [-.1425, 0, .16, .42, .6425, .8575, 1, 1.16]
#Create the arrays with the interpolated values
x = np.linspace(min(x_observed), max(x_observed), num=1000)
yR = pchip_interpolate(x_observed, yR_observed, x)
yG = pchip_interpolate(x_observed, yG_observed, x)
yB = pchip_interpolate(x_observed, yB_observed, x)
#Convert them back to python lists
x = list(x)
yR = list(yR)
yG = list(yG)
yB = list(yB)
#Find the indexs where x crosses 0 and crosses 1 for slicing
start = 0
end = 0
for i in x:
if i > 0:
start = x.index(i)
break
for i in x:
if i > 1:
end = x.index(i)
break
#Slice away the helper data in the begining and end leaving just 0 to 1
x = x[start:end]
yR = yR[start:end]
yG = yG[start:end]
yB = yB[start:end]
#Plot the values if you want
#plt.plot(x, yR, color = "red")
#plt.plot(x, yG, color = "green")
#plt.plot(x, yB, color = "blue")
#plt.show()