MATLAB中的xkcd样式图 [英] xkcd style graphs in MATLAB
问题描述
因此,才华横溢的人们已经弄清楚了如何制作 xkcd 样式图,在Python中 和
So talented people have figured out how to make xkcd style graphs in Mathematica, in LaTeX, in Python and in R already.
如何使用MATLAB生成类似于上图的图?
How can one use MATLAB to produce a plot that looks like the one above?
我创建了摆动的线条,但无法获得摆动的轴.我想到的唯一解决方案是用摆动的线覆盖它们,但是我希望能够更改实际的轴.我也无法使Humor字体工作,使用的代码位是:
I created wiggly lines, but I couldn't get wiggly axes. The only solution I thought of was to overwrite them with wiggly lines, but I want to be able to change the actual axes. I also could not get the Humor font to work, the code bit used was:
annotation('textbox',[left+left/8 top+0.65*top 0.05525 0.065],...
'String',{'EMBARRASSMENT'},...
'FontSize',24,...
'FontName','Humor',...
'FitBoxToText','off',...
'LineStyle','none');
对于摆动的线,我尝试添加一个小的随机噪声并进行平滑处理:
For the wiggly line, I experimented with adding a small random noise and smoothing:
smooth(0.05*randn(size(x)),10)
但是我无法使白色背景在它们相交时出现在它们周围...
But I couldn't make the white background the appears around them when they intersect...
推荐答案
我看到两种解决方法:第一种方法是在绘图要素的x/y坐标上添加一些抖动.这样做的好处是您可以轻松地修改图,但是如果要对其进行xkcdyfied,则必须自己绘制轴(请参见 @ Rody Oldenhuis的解决方案).第二种方法是创建一个无抖动的图,并使用imtransform
将随机失真应用于图像.这样的好处是您可以在任何绘图中使用它,但是最终将得到一个图像,而不是可编辑的绘图.
I see two ways to solve this: The first way is to add some jitter to the x/y coordinates of the plot features. This has the advantage that you can easily modify a plot, but you have to draw the axes yourself if you want to have them xkcdyfied (see @Rody Oldenhuis' solution). The second way is to create a non-jittery plot, and use imtransform
to apply a random distortion to the image. This has the advantage that you can use it with any plot, but you will end up with an image, not an editable plot.
我将首先显示#2,然后尝试显示下面的#1(如果您更喜欢#1,请查看 Rody的解决方案!).
I'll show #2 first, and my attempt at #1 below (if you like #1 better, look at Rody's solution!).
此解决方案依赖于两个关键功能:从文件交换中 EXPORT_FIG 抗锯齿的屏幕截图,然后 IMTRANSFORM 进行转换.
This solution relies on two key functions: EXPORT_FIG from the file exchange to get an anti-aliased screenshot, and IMTRANSFORM to get a transformation.
%# define plot data
x = 1:0.1:10;
y1 = sin(x).*exp(-x/3) + 3;
y2 = 3*exp(-(x-7).^2/2) + 1;
%# plot
fh = figure('color','w');
hold on
plot(x,y1,'b','lineWidth',3);
plot(x,y2,'w','lineWidth',7);
plot(x,y2,'r','lineWidth',3);
xlim([0.95 10])
ylim([0 5])
set(gca,'fontName','Comic Sans MS','fontSize',18,'lineWidth',3,'box','off')
%# add an annotation
annotation(fh,'textarrow',[0.4 0.55],[0.8 0.65],...
'string',sprintf('text%shere',char(10)),'headStyle','none','lineWidth',1.5,...
'fontName','Comic Sans MS','fontSize',14,'verticalAlignment','middle','horizontalAlignment','left')
%# capture with export_fig
im = export_fig('-nocrop',fh);
%# add a bit of border to avoid black edges
im = padarray(im,[15 15 0],255);
%# make distortion grid
sfc = size(im);
[yy,xx]=ndgrid(1:7:sfc(1),1:7:sfc(2));
pts = [xx(:),yy(:)];
tf = cp2tform(pts+randn(size(pts)),pts,'lwm',12);
w = warning;
warning off images:inv_lwm:cannotEvaluateTransfAtSomeOutputLocations
imt = imtransform(im,tf);
warning(w)
%# remove padding
imt = imt(16:end-15,16:end-15,:);
figure('color','w')
imshow(imt)
这是我最初的抖动尝试
Here's my initial attempt at jittering
%# define plot data
x = 1:0.1:10;
y1 = sin(x).*exp(-x/3) + 3;
y2 = 3*exp(-(x-7).^2/2) + 1;
%# jitter
x = x+randn(size(x))*0.01;
y1 = y1+randn(size(x))*0.01;
y2 = y2+randn(size(x))*0.01;
%# plot
figure('color','w')
hold on
plot(x,y1,'b','lineWidth',3);
plot(x,y2,'w','lineWidth',7);
plot(x,y2,'r','lineWidth',3);
xlim([0.95 10])
ylim([0 5])
set(gca,'fontName','Comic Sans MS','fontSize',18,'lineWidth',3,'box','off')
这篇关于MATLAB中的xkcd样式图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!