根据Matlab中的值用颜色绘制多条线 [英] Plot several lines with color based on a value in matlab

查看:145
本文介绍了根据Matlab中的值用颜色绘制多条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由5列组成的矩阵.第一和第二列用于x_start&该行的y_start,第三个和第四个用于x_end& y_end.第五个是-此行中的污染物浓度-给出了我的图表颜色的值.我想绘制x_start& y_start与x_end& y_end为每行,并根据浓度值在颜色图中从Cmin到Cmax的范围为该行提供一种颜色.有帮助吗?

I have a matrix consisting of 5 columns. The first and second columns are for x_start & y_start of the line, the third and fourth are for x_end & y_end. The fifth is -concentration of contaminant in this line- giving the value for the color of my graph. I want to plot x_start & y_start with x_end & y_end for each line and give this line a color based on the value of concentration which is ranging in color from Cmin to Cmax within a colormap. Any help?

推荐答案

我希望我已经正确理解了您的问题.您可以尝试以下代码.假设您的数据采用以下格式:

I hope I've understood your question correctly. You can try the following code. Assuming your data is in the following format:

%    x_start y_start x_end y_end concentration
A = [0         0      1      1     0.3
     0         1      3      3     0.6
     3         1      6      2     1.2];

并且您使用了其中一种Matlab颜色图

and you use one of the matlab colormaps

cmap = colormap;

基于最小和最大浓度(颜色图的第一个和最后一个值),您可以计算出颜色的索引

Based on a minimum and maximum concentration (first and last value of the colormap) you can calculated to indices of the colors by

con_min = 0;
con_max = 2;
ind_c = round((size(cmap,1)-1)*A(:,5)/(con_max-con_min))+1

并用

figure;
set(gca,'ColorOrder',cmap(ind_c,:),'NextPlot','replacechildren');

并使用

plot([A(:,1) A(:,3)]',[A(:,2) A(:,4)]');

要显示正确的颜色条,只需添加

To display the correct colorbar, just add

colorbar                  % Display the colorbar
caxis([con_min con_max])  % Scale it to the correct min and max

这篇关于根据Matlab中的值用颜色绘制多条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆