Assign color to values - julia

Let's say I have code that generates me a 2D array with values 0,1,2 or 3. E.g.
x = rand(Int8.(0:3), (4,4))
and now I want to plot that on a 2D grid whereas we assign each value a color, e.g.:
0: white, 1: green, 2: blue, 3: red
Example:
Assume I have the matrix
0 3
2 1
then I'd like to have the following image:
now I thought I could use heatmap but the following code
using Plots
data = [0 3; 2 1]
heatmap(data, c=cgrad([:white, :green, :blue, :red], categorical=true))
gives me
As you can see, it "plots" the values of the matrix along the axis. The axis imply an ordering of my data and thus my image gets flipped and rotated.
How can I do that?

You can write e.g.
using Plots
heatmap(x, c=cgrad([:white, :green, :blue, :red], categorical=true))
if you want to disable axis and legend and reverse the rows write (using matrix operations):
heatmap(reverse(x, dims=1),
c=cgrad([:white, :green, :blue, :red], categorical=true),
axis=nothing, legend=:none)
or using heatmap kwarg (I retain the axis to show that they are flipped)
heatmap(x, yflip=true, c=cgrad([:white, :green, :blue, :red], categorical=true))

I present a solution with Gaston.jl, which uses gnuplot as a backend.
A common way to solve a problem like this using gnuplot is to define a custom palette and plot the matrix as an image. Gaston sets up the axes so that the matrix "looks" the same mathematically and as an image; in other words, element [1,1] is drawn on the left top corner; the element on the last row and last column is drawn on the bottom right corner.
So, this works (at first sight):
using Gaston
M = [0 3; 2 1]
pal = "defined (0 'white', 1 'green', 2 'blue', 3 'red')"
imagesc(M, Axes(palette = pal, colorbox = :off, tics = :off))
A difficulty with this approach is that gnuplot scales both the arrray elements and the palette to the range [0,1]. So, the extremes of the palette are always assigned to the minimum and maximum elements of the array. So, the following example fails:
M = [1 0; 0 1];
pal = "defined (0 'white', 1 'green', 2 'blue', 3 'red')"
imagesc(M, Axes(palette = pal, colorbox = :off, tics = :off))
We would like to see two green squares instead of two red ones.
A possible solution is to programatically generate a palette that works. Assuming that we know in advance the range of (integer) elements of the array and the palette, the following function calculates a custom gnuplot palette that fits the matrix being plotted:
const colors = Dict(0 => "white", 1 => "green", 2 => "blue", 3 => "red")
function define_pal(M)
entries = unique(M) |> sort
pal = "defined ("
for e in entries
pal *= "$e '$(colors[e])'"
if e == entries[end]
pal *= ")"
else
pal *= ", "
end
end
return pal
end
Two examples:
M = [1 0; 0 1];
imagesc(M, Axes(palette=define_pal(M), colorbox=:off, tics=:off))
and
M = [1 3; 3 1];
imagesc(M, Axes(palette=define_pal(M), colorbox=:off, tics=:off))
Note that Gaston requires having gnuplot installed.

Related

GNUplot - draw line using window coordinate system

I try to plot two horizontal lines in a coordinate system using GNUPlot. The two lines represent average values in two datasets. Each dataset has the following constants: max, min and average. The same logic is to be applied to both datasets, so we can simply focus at one of them.
The output should be a 800 x 800 PNG image. They share the X axis, but their Y axis is different from each other in terms of the ranges' values and unit of measurement. Naturally, the numerical values of the two lines can vary arbitrarily. More precisely, I need to plot the two lines at, say, y = 300 and y = 500 in pixel coordinates, regardless of the value of average.
As far I as can tell, there is no way to tell GNUPlot to plot something at a specific pixel coordinate. However, I do believe it is possible to to it indirectly by adjusting the ranges to appropriate values. After poking around in GNUPlot, I managed to find proper values. When the proper range values are set, I think the datapoints in the set should be plotted nicely such that they fit into the graph. Now I need a general approach for any values.
I have the following GNUPlot script with arbitrary values for two horizontal lines:
set term png size 800, 800
set multiplot layout 1, 1
# Green line
min_green = 0
max_green = 50
set size 1,1
set ytics 20
set yrange [min_green : max_green]
avg_green = 22
plot avg_green linecolor rgb "green"
# Blue line
min_blue = 10
max_blue = 70
set size 1,1
set ytics 20
set yrange [min_blue : max_blue]
avg_blue = 14
plot avg_blue linecolor rgb "blue"
Use it like this: gnuplot -p script > plot.png
I need two procedure that looks something like this:
range_min = get_new_min_range(pixel_target_y, min, max, avg)
range_max = get_new_max_range(pixel_target_y, min, max, avg)
The ranges is put into set yrange in GNUPlot. The green line must be at y = 500 and the blue line must be at y = 300 (this is the pixel_target_y patameter). Any help is greatly appreciated!
Let me try to repeat in my words if I understood your question correctly:
You want to plot two datasets where the average (or mean) of each datasets have a fixed y-pixel-(or screen) position within the output graph (independent of data values and graph margins), correct?
For this you need the gnuplot variables GPVAL_TERM_YMIN and GPVAL_TERM_YMAX. In order to get these values you have to plot a dummy graph first. Then you need to do some calculations to get the proper range.
As you can see in the result from 3 different plots: the green and blue lines are independent of x-labels or graph titles.
Code:
### have average lines at fixed pixel position within the plot
reset session
myTermSizeX = 800
myTermSizeY = 800
set term pngcairo size myTermSizeX, myTermSizeY
myOutputFile = "Output.png"
set output myOutputFile
myFixY1 = 500
myFixY2 = 300
set title "Some graph title"
set xlabel "x-Axis title"
# create some test data
set table $Data1
plot '+' u 1:(rand(0)*50+40) smooth bezier
unset table
set table $Data2
plot '+' u 1:(rand(0)*40+10) smooth bezier
unset table
stats $Data1 u 2 name 'Data1' nooutput
stats $Data2 u 2 name 'Data2' nooutput
print Data1_min, Data1_mean, Data1_max
print Data2_min, Data2_mean, Data2_max
# dummy plot to get GPVAL_TERM_YMIN, GPVAL_TERM_YMAX
plot x
R_grph1 = real(myFixY1 - GPVAL_TERM_YMIN)/(GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)
R_grph2 = real(myFixY2 - GPVAL_TERM_YMIN)/(GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)
R_data1 = (Data1_mean - Data1_min)/(Data1_max-Data1_min)
R_data2 = (Data2_mean - Data2_min)/(Data2_max-Data2_min)
if (R_data1 > R_grph1) {
Y1min = Data1_min
Y1max = (Data1_mean - Data1_min)/R_grph1 + Data1_min
}
else {
Y1max = Data1_max
Y1min = Data1_max - (Data1_max - Data1_mean)/(1-R_grph1)
}
print Y1min,Y1max
if (R_data2 > R_grph2) {
Y2min = Data2_min
Y2max = (Data2_mean - Data2_min)/R_grph2 + Data2_min}
else {
Y2max = Data2_max
Y2min = Data2_max - (Data2_max - Data2_mean)/(1-R_grph2)
}
print Y2min,Y2max
set yrange [Y1min:Y1max]
set ytics nomirror
set y2range [Y2min:Y2max]
set y2tics nomirror
set output myOutputFile # it seems you have to specify the output again
set key top center
plot \
$Data1 u 1:2 axes x1y1 w lp pt 7 lc rgb "red" ti "Data1", \
Data1_mean axes x1y1 w l lw 2 lc rgb "green" ti "Data1 mean", \
Data1_min axes x1y1 lt 0 not, \
Data1_max axes x1y1 lt 0 not, \
$Data2 u 1:2 axes x1y2 w lp pt 7 lc rgb "orange" ti "Data2", \
Data2_mean axes x1y2 w l lw 2 lc rgb "blue" ti "Data2 mean", \
Data2_min axes x1y2 lt 0 not, \
Data2_max axes x1y2 lt 0 not
set output
### end of code
Result:

How to animate 3D scatter plot by adding each point at a time in R or MATLAB

I have a set of 3D coordinates here. The data has 52170 rows and 4 columns. Each row represent one point. The first column is point index number, increasing from 1 to 52170. The second to fourth columns are coordinates for x, y, and z axis, respectively. The first 10 lines are as follow:
seq x y z
1 7.126616 -102.927567 19.692112
2 -10.546907 -143.824966 50.77417
3 7.189214 -107.792068 18.758278
4 7.148852 -101.784027 19.905006
5 -14.65788 -146.294952 49.899158
6 -37.315742 -116.941185 12.316169
7 8.023512 -103.477882 19.081482
8 -14.641933 -145.100098 50.182739
9 -14.571636 -141.386322 50.547684
10 -15.691803 -145.66481 49.946281
I want to create a 3D scatter plot in which each point is added sequentially to this plot using R or MATLAB. The point represented by the first line is added first, then the point represented by the second line, ..., all the way to the last point.
In addition, I wish to control the speed at which points are added.
For 2D scatter plot, I could use the following code:
library(gganimate)
x <- rnorm(50, 5, 1)
y <- 7*x +rnorm(50, 4, 4)
ind <- 1:50
data <- data.frame(x, y, ind)
ggplot(data, aes(x, y)) + geom_point(aes(group = seq_along(x))) + transition_reveal(ind)
But I cannnot find information on how to do this for 3D scatter plot. Can anyone show me how this could be done? Thank you.
This is an answer for MATLAB
In a general fashion, animating a plot (or 3d plot, or scatter plot, or surface, or other graphic objects) can be done following the same approach:
Do the first plot/plot3/scatter/surf, and retrieve its handle. The first plot can incorporate the first "initial" sets of points or even be empty (use NaN value to create a plot with invisible data point).
Set axis limits and all other visualisation options which are going to be fixed (view point, camera angle, lightning...). No need to set the options which are going to evolove during the animation.
In a loop, update the minimum set of plot object properties: XData, YData ( ZData if 3D plot, CData if the plot object has some and you want to animate the color).
The code below is an implementation of the approach above adapted to your case:
%% Read data and place coordinates in named variables
csvfile = '3D scatter plot.csv' ;
data = csvread(csvfile,2) ;
% [optional], just to simplify notations further down
x = data(:,2) ;
y = data(:,3) ;
z = data(:,4) ;
%% Generate empty [plot3] objects
figure
% create an "axes" object, and retrieve the handle "hax"
hax = axes ;
% create 2 empty 3D point plots:
% [hp_new] will contains only one point (the new point added to the graph)
% [hp_trail] will contains all the points displayed so far
hp_trail = plot3(NaN,NaN,NaN,'.b','Parent',hax,'MarkerSize',2) ;
hold on
hp_new = plot3(NaN,NaN,NaN,'or','Parent',hax,'MarkerSize',6,'MarkerEdgeColor','r','MarkerFaceColor','g','LineWidth',2) ;
hold off
%% Set axes limits (to limit "wobbling" during animation)
xl = [min(x) max(x)] ;
yl = [min(y) max(y)] ;
zl = [min(z) max(z)] ;
set(hax, 'XLim',xl,'YLim',yl,'ZLim',zl)
view(145,72) % set a view perspective (optional)
%% Animate
np = size(data,1) ;
for ip=1:np
% update the "new point" graphic object
set( hp_new , 'XData',x(ip), 'YData',y(ip), 'ZData',z(ip) )
% update the "point history" graphic object
% we will display points from index 1 up to the current index ip
% (minus one) because the current index point is already displayed in
% the other plot object
indices2display = 1:ip-1 ;
set(hp_trail ,...
'XData',x(indices2display), ...
'YData',y(indices2display), ...
'ZData',z(indices2display) )
% force graphic refresh
drawnow
% Set the "speed"
% actually the max speed is given by your harware, so we'll just set a
% short pause in case you want to slow it down
pause(0.01) % <= comment this line if you want max speed
end
This will produce:

Gnuplot - Weighted point size

I have an unsorted data set of two columns with most of the points aligning diagonally along y=x, however some points misalign.
I would like to show that most of the points actually do align along the function, however just pointplotting would just overlap the over-represented points to one. The viewer would then get the impression that the data points are actually scattered randomly because there is no weight to the occurrence count.
Is there a way to implement a weight to the points that occur more than once - maybe through point size? Couldnt find anything on this topic.
Thanks a lot!
You don't show data so I assumed something from your description. As #Christoph already mentioned you could use jitter or transparency to indicate that there are many more datapoints more or less at the same location. However, transparency is limited to 256 values (actually, 255 because fully transparent you won't see). So, in extreme case, if you have more than 255 points on top of each other you won't see a difference to 255 points on top of each other.
Basically, you're asking for displaying the density of points. This reminds me to this question: How to plot (x,y,z) points showing their density
In the example below a "pseudo" 2D-histogram is created. I'm not aware of 2D-histograms in gnuplot, so you have to do it as 1D-histogram mapping it onto 2D. You divide the plot into fields and count the occurrence of point in each field. This number you use either for setting the point variable color via palette or for variable pointsize.
The code example will generate 5 ways to plot the data:
solid points
empty points
transparent points
colored points
sized points (your question)
I leave it up to you to judge which way is suitable. Certainly it will depend pretty much on the data and your special case.
Code:
### different ways to show density of datapoints
reset session
# create some random test data
set print $Data
do for [i=1:1000] {
x=invnorm(rand(0))
y=x+invnorm(rand(0))*0.05
print sprintf("%g %g",x,y)
}
do for [i=1:1000] {
x=rand(0)*8-4
y=rand(0)*8-4
print sprintf("%g %g",x,y)
}
set print
Xmin=-4.0; Xmax=4.0
Ymin=-4.0; Ymax=4.0
BinXSize = 0.1
BinYSize = 0.1
BinXCount = int((Xmax-Xmin)/BinXSize)+1
BinYCount = int((Ymax-Ymin)/BinYSize)+1
BinXNo(x) = floor((x-Xmin)/BinXSize)
BinYNo(y) = floor((y-Ymin)/BinYSize)
myBinNo(x,y) = (_tmp =BinYNo(y)*BinXCount + BinXNo(x), \
_tmp < 0 || _tmp > BinXCount*BinYCount-1 ? NaN : int(_tmp+1))
# get data into 1D histogram
set table $Bins
plot [*:*][*:*] $Data u (myBinNo($1,$2)):(1) smooth freq
unset table
# initialize array all values to 0
array BinArr[BinXCount*BinYCount]
do for [i=1:BinXCount*BinYCount] { BinArr[i] = 0 }
# get histogram values into array
set table $Dummy
plot myMax=NaN $Bins u ($2<myMax?0:myMax=$2, BinArr[int($1)] = int($2)) w table
unset table
myBinValue(x,y) = (_tmp2 = myBinNo(x,y), _tmp2>0 && _tmp2<=BinXCount*BinYCount ? BinArr[_tmp2] : NaN)
# point size settings
myPtSizeMin = 0.0
myPtSizeMax = 2.0
myPtSize(x,y) = myBinValue(x,y)*(myPtSizeMax-myPtSizeMin)/myMax*myPtSizeMax + myPtSizeMin
set size ratio -1
set xrange [Xmin:Xmax]
set yrange [Ymin:Ymax]
set key top center out opaque box
set multiplot layout 2,3
plot $Data u 1:2 w p pt 7 lc "red" ti "solid points"
plot $Data u 1:2 w p pt 6 lc "red" ti "empty points"
plot $Data u 1:2 w p pt 7 lc "0xeeff0000" ti "transparent points"
set multiplot next
plot $Data u 1:2:(myBinValue($1,$2)) w p pt 7 ps 0.5 palette z ti "colored points"
plot $Data u 1:2:(myPtSize($1,$2)) w p pt 7 ps var lc "web-blue" ti "sized points"
unset multiplot
### end of code
Result:

how to change the size, color of points in a scatter plot in R

You can find the example data in below
I want to color, recognise those points higher than 0 in another color and lower than 0 in another color. Is there any way to know which points are they ? I simply want to add a border higher and lower -1 and then say show those point higher than 1 in another color and print their name close to it while the same for lower than -1 but another color
This comment did not help since make read line randomly
x=(1:990)
cl = 1*(z>0) + 2*(z<=0)
cx = 1*(z>0) + 1.2*(z<=0)
plot(y~x, col=cl, cex=cx)
I don't want to generate red and black points around zero.
I want to detect those points higher and lower than 1 and -1 respectively.
I also want to plot them in different color and different size
Generate some data around 0:
d<-rnorm(1000,0,1)
To get the points higher than 0:
d[d>0]
To identify the index of points higher than 0:
which(d>0)
Plot points above 0 in green below 0 in red. Also, points above 0 will be a different size than points below 0:
s <- character(length(d))
s[d>0] <- "green"
s[d<0] <- "red"
# s[d > -0.5 & d < 0.5] <- "black" # to color points between 0.5 and -0.5 black
plot(d, col=s) # color effect only
sz <- numeric(length(d))
sz[d>0] <- 4 # I'm giving points greater than 0 a size of 4
sz[d<0] <- 1
plot(d, col=s, cex=sz) # size and color effect
Now, you also mention points above and below 1 and -1, respectively. You should be able to follow the code above to do what you want.
To add labels to points meeting a certain condition (e.g. greater than or less than 0.2 and -0.2, respectively), you can use the text function:
text(which(abs(d) > .2), d[abs(d) > .2], cex = 0.5, pos=3)
pos = 3 means to put the label above the point, and the cex argument to text is for adjusting the label size.
As the comments mentioned, there are many ways of doing this. Assuming that you are using the plot() function, here's a simple way of doing what you want. The key is to understand the arguments of plot(). Color of points is determined by col, size by cex, and so forth. These should all be vectors of the same size of y (else the recycling rule is used). See ?plot.
N = 999 # I don't care how many obs you have
y = rnorm(N)
# vector of colors (black for y>0, red for y<=0)
cl = 1*(y>0) + 1.2*(y<=0)
# vector of point sizes relative to default (1 for y>0, 1.2 y<=0)
cx = 1*(y>0) + 1.2*(y<=0)
plot(y, col=cl, cex=cx)
Edit:
I tried to give a general example (eg, coloring points by a third variable), but OP insists he had 2 variables. Well, just rename z by say x.
Edit:
# last edit I make
set.seed(1)
y = rnorm(N)
cl = rep(1, length(y))
cl[y > 0.5] = 2
cl[y < -0.5] = 3
plot(y, col=cl)
And here's what it gives:

gnuplot heat map color range

I have some X Y Z data in a file and I'm using gnuplot to display it.
I am creating a heat map, ie. a 2D plot where the Z value is presented using color.
Right now I'm using the following script:
set palette defined (0 "blue", 1 "red")
plot "xyz.dat" u $1:$2:$3 w image
My problem is, gnuplot ignores the 0 and 1 in the palette definition. It uses the colors specified, but rescales according to the minimal and maximal Z value in the file.
This makes it hard to visually compare different plots whose z range is different.
How do I tell gnuplot to stop rescaling the z color range?
The "set palette defined" command maps the grey values that you would get if you were plotting using greyscale onto a color palette; the scaling to min -> 0 and max -> 1 is how it's supposed to work. If you want to make a set of plots all with the same scaling of the data, you want to use the "set cbrange" command. For example,
set cbrange [0:0.5]
set palette defined (0 "blue", 1 "red")
splot '++' using 1:2:(sin($1)*cos($2)) w image
gives you an image plot with the maximum data value of 0.5 mapped to red and 0 mapped to blue.
Subsequent plots, like
splot '++' using 1:2:(0.5*sin($1)*cos($2)) w image
will use the same scaling, so they can be compared.

Resources