Plot colour of 3D point based actual value of each data point - python-3.x

I have a 3D array of slopes, X.
I would like to plot each data point and color-code the point based on the actual value. There are three possible values.
if the slope greater 80 then plot data point in red. If the slope greater -80 then blue, else green.
I would also like the legend.
Thanks
I have tried to create a couple of things but not getting it right.
#Make the array to 2D with X, Y, Z axis
X=fits.transpose(2,0,1).reshape(-1,3)
print(X.shape)
kcolors = ['red' if slope greater 0.2 elif slope greater -0.2 'green' else 'blue' for slope in X]
plt.scatter(transformed[:,0], transformed[:,1], transformed[:,2], c=kcolors)
File "<ipython-input-26-4efc22ca2a34>", line 6
kcolors = ['red' if slope > 0.2 elif slope > -0.2 'green' else 'blue' for slope in points]
SyntaxError: invalid syntax

I tried
colors = []
for slope in Xpoints[0]:
if slope > 0.2:
colors.append('r')
elif slope < -0.2:
colors.append('g')
else:
colors.append('b')
This seems to work.
Is there a better way of doing it?

Related

3D vector field in gnuplot with specified colors

I would like to plot a 3D vector field in gnuplot where every vector is a specified color. My input file looks like this:
x
y
z
dx
dy
dz
color
x1
y1
z1
dx1
dy1
dz1
color1 (hex code)
and so forth. I tried the following code, piggybacking off the solution*
scale = 1
splot 'file.dat' u 1:2:3:($4*scale):($5*scale):($6*scale):7 w vectors arrowstyle 1 lc rgb var
but this gave me a color bar on the side of the graph. Also, every vector was colored gray, instead of the color in the input file. Could someone suggest a fix for this?
* Vector field 3D plot in gnuplot with contour of vectors (bottom)
Your command is very nearly correct. The problem is that apparently the line color is considered part of the arrow style, so the program produces an error message duplicated or contradicting arguments in plot options if you give both an arrow style and a line color. If you move the lc rgb variable to the style definition, it is accepted. This works for me:
set style arrow 1 filled head lc rgb variable
plot DATA using 1:2:3:4:5:6:7 with vectors arrowstyle 1

gnuplot not showing correct scale on y axis for a circle

I am modelling a graph problem using gnuplot
I am plotting a circle with gnuplot using the following command
set xtics 1
set ytics 1
plot 'circles.txt' with circle
my circles.txt contains the follwoing data
0 0 3
the upmost point on this circle(center at origin and radius of 3 ) should be (0,3) but it is shown as (0,2) in this graph
how can i rectify this error?
Plotting with circles is intended for plotting the points as circles so they would be round regardless of axes scaling. As it is pointed in gnuplot documentation,
The radius is always interpreted in the units of the plot's horizontal axis (x or x2). The scale on y and the aspect ratio of the plot are both ignored.
You can plot with ellipses instead; from documentation on plot with ellipses:
2 columns: x y
3 columns: x y major_diam
4 columns: x y major_diam minor_diam
5 columns: x y major_diam minor_diam angle
so you plot this as
plot 'circles.txt' using 1:2:($3*2):($3*2) with ellipses
(ellipses use diameter, so the size should be the third column twice)
Or set object ellipse:
set object ellipse at 0, 0 size 6, 6

Plot line segments from a datafile with gnuplot

I have a datafile of following format (4 columns):
Size Xmid Ymid Angle
I want to plot line segments whose size is given in column 1, coordinates of the midpoint are given in columns 2 and 3, and angle with the x-axis is given in the last column.
How can I do that with gnuplot?
You can use the vectors plot style. It requires the data to be supplied as x y xdelta ydelta, where x,y denote the coordinates of the line-segment origin, and xdelta/ydelta stand for the corresponding displacements to the end point. However, this can be easily calculated from your input:
#this specifies that Gnuplot will expect angles in degrees
set angles degrees
plot 'input.dat' \
u ($2 - $1*cos($4)/2):($3 - $1*sin($4)/2):($1*cos($4)):($1*sin($4)) \
w vectors nohead \
lc rgb 'black' lw 2

Is it possible to set a gnuplot label relative to both the graph and the data?

I have a line chart where I would like to place a label with coordinates that are based on (x,y), where:
x would be relative to the graph at position (x = 0.01)
y would be relative to the data at position (value = 32)
Is it possible to display this label where it would "float" vertically depending on whether 32 is in the current yrange (in other words, if yMin < 32 < yMax, the label would be displayed, elsewise not)--such that the result would look roughly like this:
Yes, it is possible. What was tripping me up was how Gnuplot handles floating point calculations. Where I was expecting a fractional value, in reality I was getting zero. The key to my problem was the notation that Gnuplot needs in order to "think" in floating point terms:
stats dataFileForecast using 2 nooutput
freezeWarning = 32.
Yhigh = STATS_max + 10.
Ylow = STATS_min - 10.
freezeLabel = ((freezeWarning-Ylow) / (Yhigh-Ylow))
set yrange [Ylow:Yhigh]
if (32 > Ylow) set label "32°" at graph 0.01,freezeLabel tc rgb "#FFFFFF" font ",8"
The key is the 'dot' notation of the integers used in the formula to allow for the floating point calculation (someone else perhaps can explain to me why this is necessary...) The label plot coordinates are between 0 and 1 (so the middle would be 0.5) thus the need for fractional answers.
The formula "freezeLabel" computes the percentage above or below the x axis where 32 will be plotted based on the plot Yrange scale. If 32 is less than Ylow, the label won't plot (the if statement) but the math would work and just plot the label at a negative Y value (and thus, unseen.)

How to find the tangent to any pixel?

I want to find tangent at each pixel in image.
NOTE: image is having white background and shape border color is block.
What i did is,
Algo
While(true)
take pixel
if pixel color is black
make 3 X 3 matrix => fill the matrix by surrounding pixel color
...means assume white =0 and black=1 then keeping selected pixel
at center for 3 X 3 matrix and finding all other value;
----------------------------here i want to find tangent line to selected pixel;
end if
Move to next pixel.
End while
Please help Exams on head .
What you're looking for is probably a Sobel Operator. It's implemented as a convolution of the neighborhood around a pixel with the matrix:
-1 0 1
-2 0 2
-1 0 1
and again with:
-1 -2 -1
0 0 0
1 2 1
Call the results of the 2 convolutions x and y, respectively. Once you have them, you can get the magnitude of the gradient by taking the square root of the sum of the squares:
mag = sqrt(x * x + y * y);
and the direction of the gradient (which should be tangent to the pixel you're examining) by taking the arctangent of y over x:
tangent = atan2(y / x)

Resources