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))
Related
Very simple question but I'm not very familiar with the way the ColorGradient type works so I thought I'd ask here.
Say I have some x, y data that I want to plot in the standard way with scatter(x,y) and I want to color the dots with their unique colors based on a color map determined by a color gradient (see code below):
using Plots
C(g::ColorGradient) = RGB[g[z] for z=LinRange(0,1,30)]
g = :inferno
(source: https://github.com/JuliaPlots/ExamplePlots.jl/blob/master/notebooks/cgrad.ipynb)
How would I set up my color gradient object and then use it to map each of my (x,y) pairs to a different one of the colors in the color gradient, using the scatter() function?
My guess is something like:
x, y = (rand(10), rand(10))
using Plots
C(g::ColorGradient) = RGB[g[z] for z=LinRange(0,1,10)]
g = :blue
scatter(x,y,c=cgrad(g)|> C)
I just tried that and it works, but I don't really get why...what is with this |> C notation??
The shortest way to get the plot is:
using Plots, Colors
scatter(x,y,c=colormap("Blues",10))
Another colormap worth mentioning is to have very different colors of points:
scatter(x,y,c=distinguishable_colors(10))
The |> operator is just passing an argument to a function so you can write either f(x) or x |> f. Consider the example below:
julia> f(a,b=5) = a+b;
julia> 7 |> f
12
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.
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
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
I am using the EMD package for R. This package has a spectrogram function for displaying a Hilbert Spectrum (calculated with hilbertspec). The output however, is really vague and black-white.
This function does not seem to have an option for outputting color images. How can I get the spectrum displayed clearly and if possible in color.
The gray levels are hardcoded in the spectrogram function (gray()), to override this setting you could use, for instance, the following:
# define a color palette
colors <- colorRampPalette(c("#007FFF", "blue", "#000077"))
gray <- function(x) colors(255*x) # redefine gray palette
spectrogram(test1$amplitude[,1], test1$instantfreq[,1])
gray <- grDevices::gray # reset gray palette function
Another option is to use the source of the spectrogram function to define your own plot function which has an argument for the color palette.