1D plot with exciting colours

Hi there,
I have quite a simple question, simple because I'm sure Matlab can do this easily, I just don't know how.
I want to plot some 1D data - in this case my data is a change in voltage over time. But instead of a plain line plot I want a plot where the space below the line is filled with colour. I don't really care what the colours correspond to, whether it is on a spectrum so that high voltage is coloured red and low is blue, or whether whole sections under high voltage peaks are coloured red (like a spectrogram) I would just like a visually appealing plot that uses some colour map; like this one:
Although I understand that my data are no where near as detailed as this example.
Thanks for any help,
Rod.
EDIT:
As suggested, here are links to my data files...

2 件のコメント

Doug Hull
Doug Hull 2013 年 9 月 6 日
This does not look like a 1-d plot to me, it looks like an image. What is the shape (vector, matrix, 3-d matrix) of the data you are trying to visualize?
Right Grievous
Right Grievous 2013 年 9 月 7 日
Hi Doug,
I have basically got 2 vectors, very long, but vectors nonetheless. I'm aware that the example is probably an image - it's an output of the spectrogram function. I gave it more for an example of the colours in use, I'm looking for a 'gradient' of different colours rather than blocks of 2 or 3 colours.
Thanks for your help,
Rod.

サインインしてコメントする。

 採用された回答

Right Grievous
Right Grievous 2013 年 9 月 8 日
編集済み: Right Grievous 2013 年 9 月 8 日

0 投票

After some fiddling I have found this to be the simplest (and most visually appealing) solution, so I will post it here in it's simplest form for anyone else who seeks out this question/answer.
xdata = 1:1:100; % xaxis - such as time
ydata = rand(1,100); % example data
figure % open new figure
colordef black % give figure black background
patch(xdata,ydata,ydata,'FaceColor', 'interp', 'EdgeColor', 'none'); % create a patch, using x and y coordinates and use the data values themselves for colourmap
% If you want to reflect your data around the xaxis with zero being blue and peaks coloured red then use these lines instead: hold on; patch(xdata,ydata,ydata,'FaceColor', 'interp', 'EdgeColor', 'none'); patch(xdata,0-ydata,ydata,'FaceColor', 'interp', 'EdgeColor', 'none');
A similar solution can also be achieved using the scatter function - where the marker colour is defined as your ydata values. But this approach obviously means it will be a scatter graph...

その他の回答 (1 件)

A Jenkins
A Jenkins 2013 年 9 月 6 日

0 投票

I am going to assume you have a 1d vector for time and a 1d vector for voltage. If not, then please answer Doug's comment above.
Otherwise, try this:
% provide 2 arrays for your time and voltage signals
xdata=1:0.1:10; %x axis data (array of time series)
zdata=1./xdata; %z axis data (array of voltage signals)
ydata=0:max(zdata)/100:max(zdata);
[xaxis,yaxis]=meshgrid(xdata,ydata);
zaxis=repmat(zdata,length(ydata),1);
x=reshape(xaxis,1,[]);
y=reshape(yaxis,1,[]);
z=reshape(zaxis,1,[]);
scatter3(x,y,(z>y),100,255*(z>y),'filled');
view(0,90);

14 件のコメント

Image Analyst
Image Analyst 2013 年 9 月 6 日
Right Grievous
Right Grievous 2013 年 9 月 7 日
This would work, yes. But you could achieve something similar just by changing the colordef, or by changing the figure background and plotting a line in a different colour.
I'm after something more eye-catching. How could you have your blue background but in place of the red bars have a colour gradient - where 1 is red and 0 is dark blue? So in effect, fill your bars using a colourmap...
A Jenkins
A Jenkins 2013 年 9 月 7 日
If I understand your above statement correctly, you are using MATLAB's spectrogram() function. This function should create an 'exciting' plot for you - just call it without an output.
spectrogram(...,...)
instead of
s=spectrogram(...,...)
Right Grievous
Right Grievous 2013 年 9 月 7 日
Can you use the spectrogram function on 2 vectors? I have tried but couldn't work out how...
A Jenkins
A Jenkins 2013 年 9 月 7 日
編集済み: A Jenkins 2013 年 9 月 7 日
Ok, I'm still not sure I understand what you want, but I had fun drawing this, so I will post it here anyway.
1) I changed the color axis to give your gradient
2) I'm using the surf() command as an alternative to scatter3().
% provide 2 arrays for your time and voltage signals
xdata=1:0.1:10; %x axis data (array of time series)
zdata=1./xdata; %z axis data (array of voltage signals)
ydata=0:max(zdata)/100:max(zdata);
[xaxis,yaxis]=meshgrid(xdata,ydata);
zaxis=repmat(zdata,length(ydata),1);
coloraxis=255*(zaxis>yaxis).*(zaxis-yaxis)./zaxis;
surf(xaxis,yaxis,coloraxis, 'EdgeColor', 'none');
view(0,90);
Right Grievous
Right Grievous 2013 年 9 月 8 日
Hi, this example looks really good and exactly what I'm looking for. However, when I plug my vectors into your code I get a 'matrix dimensions must agree' error in the line defining the coloraxis - specifically the first '>'. I'm not really sure why this is, the code works fine for your example data, but not on mine...
Image Analyst
Image Analyst 2013 年 9 月 8 日
How can they fix it if you don't give your data?
A Jenkins
A Jenkins 2013 年 9 月 8 日
You can use the size() command on each of the matricies to see the dimensions to determine which ones don't agree. For example, your xdata and zdata should match, and your yaxis and zaxis should match.
size(xdata)
size(zdata)
Right Grievous
Right Grievous 2013 年 9 月 8 日
編集済み: Right Grievous 2013 年 9 月 8 日
Hi again,
First of all, thanks for all the help and effort you have put into this.
I managed to get the script to work (it was simply an issue with the data arranged in columns instead of rows). The figure produced has your lovely gradient effect as can be seen below, but I think my data are too sparse for this plot?
Image Analyst
Image Analyst 2013 年 9 月 8 日
Looks like a mess to me, but if you're happy with it, then whatever....
Right Grievous
Right Grievous 2013 年 9 月 8 日
編集済み: Right Grievous 2013 年 9 月 8 日
I'm not happy with it - but it is obvious the effect has worked. I'm just not sure why mine is so fragmented but the example figure is nice and filled...
Also, I have uploaded some data as you suggested.
EDIT:
Actually, looking at it again I'm not even sure the effect has worked.
Right Grievous
Right Grievous 2013 年 9 月 8 日
Wouldn't an easier way to do this just be to make an area plot and then fill the plotted data using a gradient filled patch?
Image Analyst
Image Analyst 2013 年 9 月 8 日
That sounds like a good approach if you can get it working.
Right Grievous
Right Grievous 2013 年 9 月 8 日
編集済み: Right Grievous 2013 年 9 月 8 日
I have found a solution which gives quite a nice result (and exactly the effect I was looking for) so I have 'answered' my own question.
However, I have another question - I really want to mirror this data around the xaxis, previously I did this by subtracting the values from zero and plotting on the same figure, but now I'm not sure how to get the colours in reverse too, when I plot it this way now I get blue at the lowest negative number and red at the highest positive, I really want blue at zero and red at high negatives and positives...
EDIT:
I solved this too and added it to my answer. Thanks everybody for the help!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeColor and Styling についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by