简体   繁体   English

Matlab中x轴刻度不等距的散点图

[英]Scatter plot in Matlab with x-axis ticks not equidistant

Consider the following scatter plot in Matlab 考虑以下Matlab中的散点图

A=[1 0 0 0 2 0 0 0 0 0 0 0 1 2 0 0 0 2 1 200 300]';
xRange = 0: max(A);  
prob=zeros(size(xRange,2),1);
for r=1:size(xRange,2)
prob(r) = sum(ismember(A,xRange(r)))/size(A,1); 
end

scatter(xRange,prob, 'b');  
xlim([-2 max(A)+2])
ylim([-0.5 1.5])

I want to change the way the scatter looks like in order to make it more clear: the idea is to put the following ticks on the x-axis 我想更改散点图的方式以使其更清晰:其想法是在x轴上放置以下刻度

[-0.5 0 1 2 3 301]

but the tricky part is that they should be equidistant so that I can zoom on the part of the scatter plot with higher values of prob . 但是棘手的是他们应该等距,这样我可以用较高的prob值放大散点图的部分。

Any idea? 任何想法?

One way to achieve this is to transform the data to a new scale using interpolation. 实现此目的的一种方法是使用插值将数据转换为新的比例。 Let's say you want the data values 假设您要数据值

tickVal = [-0.5 0 1 2 3 301];

to appear at the graphical positions 出现在图形位置

tickPos = 0 : 5;

Determine the graphical positions for all the data by interpolation, and plot the transformed data: 通过插值确定所有数据的图形位置,并绘制转换后的数据:

xTransformed = interp1(tickVal, tickPos, xRange);
scatter(xTransformed, prob, 'b');  
xlim([min(tickPos), max(tickPos)])
ylim([-0.5 1.5])

Now we have to make sure that the ticks do not reflect the transformed, but the original data values: 现在,我们必须确保刻度线不反映转换后的值,而是原始数据值:

set(gca, 'XTick', tickPos)
set(gca, 'XTickLabel', num2cell(tickVal))

The result looks like this: 结果看起来像这样:

Is this what you want? 这是你想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM