Gnuplot color interpolation for set of linear functions - graph

I want to plot N different linear functions in a graph using gnuplot.
Furthermore, I have to colors, lets say red and black.
I want to plot all functions with different colors, so that the first function is red, the Nth is black, and the color of all functions in between is interpolated.
How can I do this using gnuplot?
Note: N is not fixed, so I would like gnuplot to do the interpolation.
Something like this, which I quickly hacked together in Paint:

Here is one possibility to color the lines according to a predefined palette
N=6
set palette defined (0 'red', 1 'black')
f(x, n) = x+n
set samples 100
set style data lines
set key left
plot for [i=0:(N-1)] f(x, i) lw 2 lt palette frac i/(N-1.0) title sprintf('n = %d', i)
linetype palette frac chooses the color from a defined palette using a fractional value. You could also use linetype palette cb to use absolute values.
The result with 4.6.4 is

Related

Apply colormap when plotting array in Julia

I am trying to apply a colormap when plotting an array of numbers in Julia 1.4.1, but all plot entries appear to only use the first color in the map. For instance:
using Plots
plot([1:10], rand(10,5), c=:viridis)
No colour change occurs even when I increase the number of plot entries to 1000:
plot([1:10], rand(10,1000), c=:viridis)
I have tried to apply the map using color or cgrad too, but had no success. So this leaves me wondering: how can I apply this colourmap such that it correctly spans the number of plot entries?
Do you want each line to use a different color?
I think you are looking for the palette argument.
First,create the color palette based on viridis, as suggested by Anshul in the comments (see end for alternative method).
julia> using Plots
julia> p = Plots.palette(:viridis, 11)
In the last code, 11 is the number of lines
Now, let's plot using the palette argument.
julia> plot( [1:10], rand(10,11), palette=p )
Alternative method to create the palette is creating a color gradient function
julia> C(g::ColorGradient) = RGB[ g[z] for z in range(0,stop=1,length=11) ]
We define the palette using that function
julia> g = :viridis
julia> p = C(cgrad(g))

Specifying a specific hex color for individual points in a 3d scatter plot

I have LiDAR dataset on which I've extracted RGB-NIR colors from a raster. I'm having no issues generating the 3d plots in plotly I want to make, or coloring the plot based on a 1-d column of values (such as NDVI); but I'm interested in coloring the points based on an RGB hex code I've written into one of the columns (taken from the raster), but i can't seem to find a way to write this in as a colorspace. While I can call on individual hex codes, it treats them as factors rather than a color specification. What I've figured out that I can do is either a) specify a qualitative color, or b) apply a color ramp. I can't seem to find a way to apply an RGB colorspace to the data.
Is there a way to specify a column in a data.frame (working in R) which has a hex or RGB code associated with each individual point in a plotly scatter plot? Is my only alternative to make a 3 color (R-G-B) color ramp, and map colors to it? Is there any way to get RGB colors to a plotly marker?
stack.exa<-data.frame(X= c(470219.59,470203.74,470215.49,470218.41,470214.88),
Y= c(5013443.99,5013439.34,5013432.39,5013437.72,5013436.6),
Z= c(645.62,629.83,638.78,617.32,639.54),
R= c(49,88,69,68,64),
B= c(76,134,102,97,96),
G= c(69,91,78,75,79),
HEX=c("#314545","#585B5B","#454E4E","#444B4B","#404F4F"))
trace.test<-plot_ly(stack.exa,
x = ~X, y = ~Y, z = ~Z,
color=~HEX,
marker=list(
size = 20,
line = list(color = 'rgba(0, 0, 0, 0)',
width = 0)
)
)
According to ?plot_ly():
color (...) To avoid scaling, wrap with I()
Modifying your code to color =~ I(HEX) seems to work. It's hard for me to tell, though, since all of the colors appear to be very similar to one another (Five Shades of Grey...)

Rescaling colors palette in r

In R i have a cloud of data around zero ,and some data around 1, i want to "rescale" my heat colors to distinguish lower numbers.This has to be done in a rainbow way, i don't want "discrete colors".I tried with breaks in image.plot but it doesn't work.
image.plot(X,Y,as.matrix(mymatrix),col=heat.colors(800),asp=1,scale="none")
I tried :
lowerbreak=seq(min(values),quantile2,len=80)
highbreak=seq(quantile2+0.0000000001,max(values),len=20)
break=c(lowerbreak,highbreak)
ii <- cut(values, breaks = break,
include.lowest = TRUE)
colors <- colorRampPalette(c("lightblue", "blue"))(99)[ii]
Here's an approach using the "squash" library. With makecmap(), you specify your colour values and breaks, and you can also specify that it should be log stretched using the base parameter. It's a bit complex, but gives you granular control. I use it to colorize skewed data, where I need more definition in the "low end".
To achieve the rainbow palette, I used the built-in "jet" colour function, but you can use any colour set - I give an example for creating a greyscale ramp with "colorRampPalette".
Whatever ramp you use, it will take some playing with the base value to optimize for your data.
install.packages("squash")
library("squash")
#choose your colour thresholds - outliers will be RED
minval=0 #lowest value to get a colour
maxval=2.0 #highest value to get a colour
n.cols=100 #how many colours do you want in your palette?
col.int=1/n.cols
#create your palette
colramp=makecmap(x=seq(minval,maxval,col.int),
n=n.cols,
breaks=prettyLog,
symm=F,
base=10,#to give ramp a log(base) stretch
colFn=jet,
col.na="red",
right=F,
include.lowest=T)
# If you don't like the colFn options in "makecmap", define your own!
# Here's an example in greyscale; pass this to "colFn" above
user.colfn=colorRampPalette(c("black","white"))
Example for using colramp in a plot (assuming you've already created colramp as above somewhere in your program):
varx=1:100
vary=1:100
plot(x,y,col=colramp$colors) #colors is the 2nd vector in the colramp list
To select specific colours, subset from the list via, e.g., colors[1:20] (if you try this with the example above, the first colors will repeat 5 times - not really useful but you get the logic and can play around).
In my case, I had a grid of values that I wanted to turn into a coloured raster image (i.e. colour mapping some continuous data). Here's example code for that, using a made up matrix:
#create a "dummy matrix"
matx=matrix(data=c(rep(2,50),rep(0,500),rep(0.5,500),rep(1,500),rep(1.5,500)),nrow=50,ncol=41,byrow=F)
#transpose the matrix
# the output of "savemat" is rotated 90 degrees to the left
# so savemat(maty) will be a colorized version of (matx)
maty=t(matx)
#savemat creates an image using colramp
savemat(x=maty,
filename="/Users/KeeganSmith/Desktop/matx.png",
map=colramp,
outlier="red",
dev="png",
do.dev.off=T)
When using colorRampPalette, you can set the bias argument to emphasise low (or high) values.
Something like colorRampPalette(heat.colors(100),bias=3) will result focus the 'ramp' on the lower, helping them to be more visually distinguishable.

Colormap in 2d plots in Scilab

When using the plot(y) function in Scilab, being y a real matrix, multiple data curves are plotted on a 2d plot and the color of each curve is automatically set.
According to Scilab online help, a default color table is used (the command cycles the table and colors each curve accordingly):
http://help.scilab.org/docs/5.5.2/en_US/plot.html
The problem is that the default table lists only 7 colors, so the 8th data curve will have the same color as the 1st, and so on.
Is there a way to extend this table, to automatically color more than 7 data curves with distinct colors?
I tried using the colormap as in 3d plots, but didn't worked.
f = scf();
plot(myData);
f.color_map = jetcolormap(32);
I think this only works for 3d plots.
You can use a for loop to set the foreground color using gce().
Example
clf()
number_of_points=100;
number_of_lines=42;
x=[1:number_of_points]
for line_number = 1:number_of_lines
plot(x, x+line_number);
last_line = gce();
last_line.children.foreground = line_number;
end
f = gcf();
f.color_map= jetcolormap(number_of_lines);
Result

How do I select a color for every point in Gnuplot data file?

I want to plot points, each with X, Y and a color. How do I do that in Gnuplot?
You can try something like this:
Line plot in GnuPlot where line color is a third column in my data file?
For example:
plot "./file.dat" u 1:2:3 with points palette
where file.dat contains your data, the first column is the x axis and the second column is the y axis, the third column is the colour.
You could consider looking at the Pyxplot plotting package http://pyxplot.org.uk, which has very similar syntax to gnuplot (albeit cleaned up considerably), and which allows point styles to be specified on a point-by-point basis. For example, in Pyxplot:
plot "file.dat" using 1:2:3 with points color rgb($4,$5,$6)
would take the RGB components for the color of each point from the 4th, 5th and 6th columns of the data file. Alternatively,
plot "file.dat" using 1:2:3 with points color $4
would read the numbers in the 4th column of the data file (call it n), and plot each point with the n-th color from the palette. If n is non-integer, say 2.5, it gives you a color half way between color 2 and color 3 in RGB space.

Resources