【问题标题】:How do you fill the area above a curve of points with one color and below the same curve with a different color in MATLAB?如何在 MATLAB 中用一种颜色填充点曲线上方的区域,并用不同颜色填充同一曲线下方的区域?
【发布时间】:2020-05-19 20:19:37
【问题描述】:

在 MATLAB 中,我有以下几组点:

T_arr =
        1000
         500
         400
         300
         200
         100

results =

    2.6000    
    2.2000    
    2.1500    
    2.1000    
    2.0000    
    1.8000 

当我绘制它们时,它看起来像这样:

plot(T_arr, results); hold on; 
plot(T_arr, results,'*'); 
xlabel('T'); 
ylabel('result'); 
title('T vs. result')

我想用一种颜色(比如红色)为曲线上方的区域着色,用另一种颜色(比如蓝色)为曲线下方的区域着色。在 MATLAB 中如何做到这一点?

我知道 MATLAB 中有两个函数称为 fillarea ,但我不清楚如何将它们用于这个特定问题。

【问题讨论】:

    标签: matlab plot matlab-figure


    【解决方案1】:

    要使用fill,我们需要提供一组形成闭合形状的点。 我们将使用您的数据集中的点以及图片的左上角 (x0,y0) 和右下角 (x1, y1)。

    现在我们需要找到这些坐标并为两组不同颜色的点调用填充(fill 中的最后一个参数)。如果数据已排序或已知限制,您可以避免使用 maxmin。我在线条之前绘制区域以确保标记可见——这也是我不使用红色和蓝色的原因。

    T_arr = [1000, 500, 400,300, 200, 100];
    results =[2.6000,    2.2000,   2.1500,    2.1000,    2.0000, 1.8000 ];
    plot(T_arr, results); hold on; 
    x0 = min(T_arr);
    y0 = max(results);
    x1 = max(T_arr);
    y1 = min(results);
    fill([x0, T_arr],[y0, results],'g');
    fill([x1, T_arr],[y1, results],'y');
    plot(T_arr, results,'*'); 
    xlabel('T'); 
    ylabel('result'); 
    title('T vs. result');
    

    结果如下:

    【讨论】:

    • 完美。这正是我一直在寻找的。谢谢。
    猜你喜欢
    • 2016-12-28
    • 2021-06-23
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    相关资源
    最近更新 更多