如何在MATLAB中为轮廓图放置轮廓上的字符串标签 [英] How To Put String Labels on Contours for Contour Plots in MATLAB

查看:131
本文介绍了如何在MATLAB中为轮廓图放置轮廓上的字符串标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以用一组用户定义的字符串标记MATLAB轮廓图的轮廓?

I am wondering if it is possible to label the contours of a MATLAB contour plot with a set of user-defined strings?

我当前正在使用以下代码截取器来生成标记的轮廓图:

I am currently using the following code snipper to produce a labelled contour plot:

%Create Data
X = 0.01:0.01:0.10
Y = 0.01:0.01:0.10
Z = repmat(X.^2,length(X),1) + repmat(Y.^2,length(Y),1)';

%Create Plot 
hold on
[C,h] = contourf(X,Y,Z);

%Add + Format Labels to Plot
hcl = clabel(C,h,'FontSize',10,'Color','k','Rotation',0);
set(hcl,'BackgroundColor',[1 1 1],'EdgeColor',[0 0 0],'LineStyle','-',)
hold off

此代码的问题是标签是由MATLAB自动生成的.即使我可以轻松更改标签的轮廓,也无法更改它们得到的标签.

The issue with this code is that the labels are automatically generated by MATLAB. Even as I can easily change the contours that are labels, I cannot change the labels that they get.

理想情况下,我想用一组我自己定义的字符串来标记它们.但是,如果这不可能,那么我想知道是否可以更改标签的数字格式.原因是上面的代码实际上生成了错误率的轮廓图,我希望将其显示为%值(即在轮廓标签中使用1%,而不是0.01等).

Ideally, I would like to label them with a set of strings that I define myself. However if that is not possible, then I am wondering if it is possible to change the numeric format of the labels. The reason for this is that the code above actually produce a contour plot for an error rate, which I would like to display as a % value (i.e. use 1% in the contour label, instead of 0.01 etc.).

推荐答案

在这种情况下,hcl实际上是一个数组,用于存储绘图上每个轮廓标签的句柄.当您使用数组设置属性时(如代码中一样),

In this case, hcl is actually an array which stores handles to every contour label on your plot. When you set properties using the array (as in your code),

set(hcl, 'name', 'value')

您将每个标签的属性设置为相同的值.

You will set the property of every label to the same value.

您可以通过遍历数组来更改单个标签的属性.例如,这是您添加百分号的方式:

You can change the properties of individual labels by iterating over the array. For example, this is how you would add a percentage sign:

for i = 1:length(hcl)
    oldLabelText = get(hcl(i), 'String');
    percentage = str2double(oldLabelText)*100;
    newLabelText = [num2str(percentage) ' %'];
    set(hcl(i), 'String', newLabelText);
end

这篇关于如何在MATLAB中为轮廓图放置轮廓上的字符串标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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