How to add different colors to one line chart

I have a line plot, and i need it to change colour depending on some criteria.
I have the chart below, adn i want this charge to have different colors at different areas.
I've shown two charts below.
I want to change the colors of the top chart, depending on where the bottom chart is.
For eg. if the bottom chart is near 0, then show blue, if the bottom chart is up near 500 make the color of the top chart red, if the bottom chart is down near -500, then make the color of the top chart green.
All the documentation i've seen is about adding new lines with different colors, i don't want that, i want one line with multiple colors.
If someone can assist, that would be great.

1 Comment

Here are two demos that produce what appears to be a single line with varying color.
If you want to get into undocumented methods,
But one of the functions Walter Roberson mentioned is probably the easiest route for your task.

Sign in to comment.

 Accepted Answer

Hello Rizwan,
The plot you asked about seems to be using the scatter function instead of plot function in MATLAB.
The scatter function can take 3 arguments: the values of the x-axis, the values of the y-axis and the colormap of the dots. For example:
fakedata = cumsum( randn(1000, 1) );
depth = linspace(1,10,1000);
scatter(1:1000,fakedata,100,depth,'filled') % 100 relates to the size of each dot
Even though the data is just random numbers, the interpretation of such a graph is that there is a negative correlation between the x-axis and y-axis (as x values increase, y values tend to decrease) and the 'depth' represented by the colors also has a negative correlation with the y-axis (as the colors get hotter, i.e. increase in value, the y values tend to decrease).

3 Comments

True, every point on a scatter plot can be a different color. But the user asked for a line plot.
Thanks Both,
This is great, this is just what I needed.
Correct I asked for plot, but only because I didn't know other options existed, what I wanted was whatever solved the problem.
Thanks to both for your great feedback, truly appreciated
@Vinicius
Thanks to your help, i've been able to solve the problem, i wanted to share my results below:
Here is my stock data:
I then detrend the data, here is the detrended data
Then using your help, i scatter plot the stock data, using a colour map based on the detrended data as shown below
depth = detrend_sdata;
scatter(t,sdata,30,depth,'filled')
colorbar;
colormap jet;

Sign in to comment.

More Answers (1)

It is not possible in MATLAB for one Chart Line Object to have multiple colors.
For multiple colors you have a few possibilities:
  • multiple line objects
  • create a surface() with careful control over node colors and edge coloring configuration.
  • create a patch() with careful control over node colors and edge coloring configuration.

4 Comments

Dear Sir,
So if i want to make a chart like below, its not possible?
@Walter,
Thanks for sending through those links, however, they are so hard to follow, that i'm unsure ill be able to get it done.
The link is kind of like my image above, however, they have like three variables x,y,z i only have two (x,y)
Then they have other metrics on color, and i have on idea what there looking for their
If there is a video on this, that would be highly appreciated it.
You can use zero for the z.
So if i want to make a chart like below, its not possible?
It is not possible with a single Chart Line Object (more commonly known as a line() object.)
The underlying graphics system (OpenGL) that transforms coordinates into graphics, just does not support drawing multicolored lines as one of its "primitive" operations.
There are other ways to draw graphs like the one you picture -- but those ways do not involve using a single Chart Line Object.
Using the first of the links I gave, color_line3:
color_line3(days, stock_price, zeros(size(stock_price)), detrended_stuff)

Sign in to comment.

Categories

Products

Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!