Matlab:柱状图饼状图填充不同条纹

作者&投稿:攸冯 (若有异议请与网页底部的电邮联系)
~ Matlab是强大的工具,是众所周知的画图仿真工具;
Matlab不仅可以画柱状图,饼状图,曲线图,直方图等二维图型;而且可以画三维或多维图。
这里主要介绍在柱状图和饼状图内填充条纹,由于Matlab画图工具箱内无填充工具,这里主要通过命令来实现。

导入几组数据,通过命令得到柱状图或者饼状图。 示例:以导入txt文件格式为例;

弹出柱状图界面和饼状图界面。 可见,柱状图和饼状图为彩色图片,有时我们不要彩色图片,要黑白图像。 对黑白图像如何区分不同类别的数据,就需要往图像里面填充不同的条纹。

打开编辑—图形属性, 更改自己所需要的字体,坐标轴范围,图例,线宽等; 也可用命令修改,如:

下面就是重点:如何把上述图像转换成黑白图像,并填充不同条纹。 首先,创建名为“applyhatch.m”的函数脚本。 在同一路径下。 代码如下:——复制即可。 function applyhatch(h,patterns,colorlist) %APPLYHATCH Apply hatched patterns to a figure %APPLYHATCH(H,PATTERNS) creates a new figure from the figure H by %replacing distinct colors in H with the black and white %patterns in PATTERNS. The format for PATTERNS can be %a string of the characters '/', '\', '|', '-', '+', 'x', '.' %a cell array of matrices of zeros (white) and ones (black) % %APPLYHATCH(H,PATTERNS,COLORS) maps the colors in the n by 3 %matrix COLORS to PATTERNS. Each row of COLORS specifies an RGB %color value. % %Note this function makes a bitmap image of H and so is limited %to low-resolution, bitmap output. % %Example 1: %bar(rand(3,4)); %applyhatch(gcf,'\-x.'); % %Example 2: %colormap(cool(6)); %pie(rand(6,1)); %legend('Jan','Feb','Mar','Apr','May','Jun'); %applyhatch(gcf,'|-+.\/',cool(6)); % %See also: MAKEHATCH %By Ben Hinkle, bhinkle@mathworks.com %This code is in the public domain.  oldppmode = get(h,'paperpositionmode'); oldunits = get(h,'units'); set(h,'paperpositionmode','auto'); set(h,'units','pixels'); figsize = get(h,'position'); if nargin == 2 colorlist = []; end bits = hardcopy(h,'-dzbuffer','-r0'); set(h,'paperpositionmode',oldppmode); bwidth = size(bits,2); bheight = size(bits,1); bsize = bwidth * bheight; if ~isempty(colorlist) colorlist = uint8(255*colorlist); [colors,colori] = nextnonbw(0,colorlist,bits); else colors = (bits(:,:,1) ~= bits(:,:,2)) | ... (bits(:,:,1) ~= bits(:,:,3)); end pati = 1; colorind = find(colors); while ~isempty(colorind) colorval(1) = bits(colorind(1)); colorval(2) = bits(colorind(1)+bsize); colorval(3) = bits(colorind(1)+2*bsize); if iscell(patterns) pattern = patterns{pati}; elseif isa(patterns,'char') pattern = makehatch(patterns(pati)); else pattern = patterns; end pattern = uint8(255*(1-pattern)); pheight = size(pattern,2); pwidth = size(pattern,1); ratioh = ceil(bheight/pheight); ratiow = ceil(bwidth/pwidth); bigpattern = repmat(pattern,[ratioh ratiow]); if ratioh*pheight bheight bigpattern(bheight+1:end,:) = []; end if ratiow*pwidth bwidth bigpattern(:,bwidth+1:end) = []; end bigpattern = repmat(bigpattern,[1 1 3]); color = (bits(:,:,1) == colorval(1)) ... (bits(:,:,2) == colorval(2)) ... (bits(:,:,3) == colorval(3)); color = repmat(color,[1 1 3]); bits(color) = bigpattern(color); if ~isempty(colorlist) [colors,colori] = nextnonbw(colori,colorlist,bits); else colors = (bits(:,:,1) ~= bits(:,:,2)) | ... (bits(:,:,1) ~= bits(:,:,3)); end colorind = find(colors); pati = (pati + 1); if pati length(patterns) pati = 1; end end newfig = figure('units','pixels','visible','off'); imaxes = axes('parent',newfig,'units','pixels'); im = image(bits,'parent',imaxes); fpos = get(newfig,'position'); set(newfig,'position',[fpos(1:2) figsize(3) figsize(4)+1]); set(imaxes,'position',[0 0 figsize(3) figsize(4)+1],'visible','off'); set(newfig,'visible','on'); function [colors,out] = nextnonbw(ind,colorlist,bits) out = ind+1; colors = []; while out = size(colorlist,1) if isequal(colorlist(out,:),[255 255 255]) | ... isequal(colorlist(out,:),[0 0 0]) out = out+1; else colors = (colorlist(out,1) == bits(:,:,1)) ... (colorlist(out,2) == bits(:,:,2)) ... (colorlist(out,3) == bits(:,:,3)); return end end %而applyhatch函数需要调用下面的函数 function A = makehatch(hatch) %MAKEHATCH Predefined hatch patterns %MAKEHATCH(HATCH) returns a matrix with the hatch pattern for HATCH %according to the following table: %HATCHpattern %---------------- %/right-slanted lines %\left-slanted lines %|vertical lines %-horizontal lines %+crossing vertical and horizontal lines %xcriss-crossing lines %.single dots % %See also: APPLYHATCH %By Ben Hinkle, bhinkle@mathworks.com %This code is in the public domain. n = 6; A=zeros(n); switch (hatch) case '/' A = fliplr(eye(n)); case '\' A = eye(n); case '|' A(:,1) = 1; case '-' A(1,:) = 1; case '+' A(:,1) = 1; A(1,:) = 1; case 'x' A = eye(n) | fliplr(diag(ones(n-1,1),-1)); case '.' A(1:2,1:2)=1; otherwise error(['Undefined hatch pattern "' hatch '".']); end
柱状填充图和饼状填充图,画图命令如下: a=importdata('xxx.txt'); b=importdata('xxx.txt'); c=importdata('xxx.txt'); d=[a,b,c]; bar(d); 警告: MATLAB 已通过改用 OpenGL 软件禁用了某些高级的图形渲染功能。欲了解有关详细信息,请点击此处。 figure(2) pie(d); axis([0 11 0.0 22]); legend('方法1','方法2','方法3'); set(gcf,'color','white'); applyhatch(gcf,'x+\.');

结果:不同条纹填充结果 柱状图和饼状图 如图所示:


MATLAB怎么给柱状图加数据标签
在MATLAB中,为柱状图添加数据标签相当直接。首先,启动MATLAB应用程序,打开命令行窗口,这是你编写代码的主要界面。在命令行中,你需要输入特定的代码来创建和定制你的柱状图。例如,你可以开始输入如下代码,来指定图形的名称,就像这样:matlabfigure('Name','YourChartTitle')接着,对于轴标签,使用类似...

如何利用matlab画柱状图
1、首先打开matlab软件声明一个X坐标轴的数据,如下图所示 2、接下来在声明一个Y坐标轴的数据,如下图所示 3、然后执行bar方法,在括号中传入X,Y坐标轴的数据,如下图所示 4、最后我们就可以画出一个柱状图了,如下图所示 工具\/材料 matlab ...

Matlab:柱状图饼状图填充不同条纹
Matlab不仅可以画柱状图,饼状图,曲线图,直方图等二维图型;而且可以画三维或多维图。这里主要介绍在柱状图和饼状图内填充条纹,由于Matlab画图工具箱内无填充工具,这里主要通过命令来实现。导入几组数据,通过命令得到柱状图或者饼状图。 示例:以导入txt文件格式为例;弹出柱状图界面和饼状图界面。 可见...

matlab怎么画柱状堆叠图
1、首先我们打开3名学生(Amy,Jacqualine,Rory)的考试成绩数据。2、将上述3名学生的考试成绩创建为成结构数组。启动MATLAB,新建脚本(Ctrl+N),输入图示代码,其中Student就是创建的结构数组,该结构数组中包含了3名学生的姓名,考试科目和考试成绩。3、保存和运行上述脚本,在工作区(Workspace)就会...

matlab画柱状图不从0开始
matlab画柱状图不从0开始可以使用函数ylim。默认情况下,这些函数会从0开始绘制柱子。但是,如果希望柱状图不从0开始,可以使用ylim函数来自定义Y轴的范围。

matlab中绘制柱状图时用bar(Y,’stack‘)是怎么改各部分颜色?
1、打开matlab,新建一个脚本m文件,绘制出一个柱状图。2、运行m文件之后,可以看到默认图形背景颜色为白色。3、通过“backColor = [0.1 0.8 0.2];set(gca, 'color', backColor);”设置图形背景颜色,其中[0.1 0.8 0.2]是RGB颜色值,全部在0-1之间。4、运行脚本,可以看到图形背景颜色...

如何用matlab画带有均值和方差的柱状图
a=[8 9 10 7 8 9];%mean b=[1 1 1 1 1 1];%std figure(); bar(a,'c'); hold on; errorbar(a,b,'k','LineStyle','none');matlab画带有均值和方差的柱状图代码

matlab三维柱状图高度怎么调
如何根据条形高度为条形着色,以此方式来修改三维条形图。创建使用 magic 函数得到的数据的三维条形图。在数组 b 中返回用于创建条形图的曲面对象。向图形添加颜色栏。命令行键入:Z = magic(5);命令行键入:b = bar3(Z);命令行键入:colorbar 对每个曲面对象,从 ZData 属性取得 z 坐标数组。使用该...

怎么用MATLAB 画三维柱状图
bar3语句 :bar3(Y)bar3(x,Y)可尝试代码:bar3(magic(5));colormap(jet);colorbar;

matlab男生女生比例用一个柱状图怎么画
1、给一张柱状图,注意AB两个对比matlab男生女生比例的数据,是分别对应一个数组的每一列,然后填写代码。2、是在柱状图上面加对应的数据,然后再填写不同的代码。

河口区17328962092: 怎么使matlab里的柱状图有不同的填充图案 -
戈烁平欣: applyhatch函数可以将颜色替换成形状填充,但是化成的图像却不是矢量图,失真的严重...暂时没找到合理的方法

河口区17328962092: matlab怎么画柱状堆叠图 -
戈烁平欣: 画柱状填充图 代码: data = [96.3,92.6,71.2;95.7,93.6,83.9;96.8,94.3,78.3;95.8,92.7,80.3]; bar(data,1); axis([0 6 0.0 100]); legend('方法','exited','Square'); set(gca,'XTickLabel',{'Img1','Img2','Img3','Img4'}); set(gcf,'color','white')applyhatch(gcf,'\.x.'...

河口区17328962092: matlab 如何在饼图上既有比例又有标注 -
戈烁平欣: x=numE(1:end)./1e4;% cost for i=3:1:(length(numE)+2)label1{i-2,1} = txtE{i,1};% label end ax1 = subplot(1,2,1); explode1=[zeros(1,length(x)-1),1]; p=pie(ax1,x,explode1); title(ax1,'Main-Cost Distribution'); %%以下为比例+标注 pText = findobj(p,'...

河口区17328962092: 如何利用matlab画柱状图 -
戈烁平欣: MATLAB画柱状图使用的是bar函数 bar(x,y) 画出的是以x为横轴,y为纵轴的柱状图.

河口区17328962092: 求matlab编程试用两种方法绘制饼图,数据为0.5,1,1.6,1.2,0.8,2.1. -
戈烁平欣: x = [0.5 1 1.6 1.2 0.8 2.1]; pie(x) pie3(x)

河口区17328962092: matlab 画柱状图问题. -
戈烁平欣: 可以用stem函数,分两次画,分别指定线型、颜色:stem(1,0.5,'fill','r--'); hold on stem([2 3 4 5],[0.2 0.1 0 0.2],'fill','b'); axis([0 6 0 1])

河口区17328962092: 你会用matlab画饼状图比大小???用matlab画柱状图比大小??? -
戈烁平欣: pie([2 4 3 5],{'North','South','East','West'}) 上面是画饼图 画柱图用bar bar([3,4,7,2,1]) ============== 比较大小,我刚才就没有理解什么意思?

河口区17328962092: 关于Highcharts饼图的颜色填充 -
戈烁平欣: 右键点击柱状图,数据系列格式,然后内部颜色填充

河口区17328962092: 急急急!!!!谁会用matlab画饼状图比大小???用matlab画柱状图比大小???
戈烁平欣: 代码: % 急急急!!!!谁会用matlab画饼状图比大小??? % 用matlab画柱状图比大小??? clc; clear all; close all; x = [1 3 0.5 2.5 2]; explode = [0 1 0 0 0]; figure; pie(x,explode) colormap jet figure; Y = randn(3,5); h = bar(Y); 结果:

河口区17328962092: matlab中绘制柱状图时用bar(Y,'stack')是怎么改各部分颜色?
戈烁平欣: <p>b=bar(...);</p> <p>set(b,'facecolor',[x y z]);% x y z 设置不同的值为不同颜色 如黑色[0 0 0]</p> <p> </p> <p>that's all, no 3qu.</p> <p></p>

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网