matlab画柱状图并填充斜线_matlab画柱状图分两类

matlab画柱状图并填充斜线_matlab画柱状图分两类导读:记录一下如何使用matlab画柱状图,并进行填充。版本:matlab2017b.注意:使用matlab2016版本似乎会有bug。1.辅助函数makehatch.mfunctionA=makehatch(hatch)%MAKEHATCHPredefinedhatchpatterns%MAKEHATCH(HATCH)returnsamatrix…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

导读:记录一下如何使用matlab画柱状图,并进行填充。
版本:
    matlab 2017b.
注意:使用matlab2016版本似乎会有bug。

1.辅助函数makehatch.m

function A = makehatch(hatch)
%MAKEHATCH Predefined hatch patterns
%  MAKEHATCH(HATCH) returns a matrix with the hatch pattern for HATCH
%   according to the following table:
%      HATCH        pattern
%     -------      ---------
%        /          right-slanted lines
%        \          left-slanted lines
%        |          vertical lines
%        -          horizontal lines
%        +          crossing vertical and horizontal lines
%        x          criss-crossing lines
%        .          single dots
%
%  See also: APPLYHATCH

%  Copyright 2002-2009 The MathWorks, Inc.

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

2.辅助函数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

%  Copyright 2002-2009 The MathWorks, Inc.
  
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
if verLessThan('matlab','8.4.0')
  bits = hardcopy(h,'-dzbuffer','-r0');
else
  bits = print(h,'-RGBImage','-r0');
end
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

3.画柱状图的.m文件,其中调用了applyhatch()函数

%If you want to adjust the pattern to 6 bar such as " applyhatch(gcf,'.-+/|x');",
%try to type this "applyhatch(gcf,'.-++/||xx');" instedly. 
%So you can avoid the duplicated pattern at least, even order problem is still not solved. 
data=[345,359,209;155,161,99];%三列的柱状图,分为了两种,具体见图

b=bar(data);%画柱状图

text(0.73,345+12,'345');%设置柱状图上的数值大小
text(0.73+0.23,359+12,'359');
text(0.73+0.45,209+12,'209');

text(0.73+1,155+12,'155');
text(0.73+1+0.23,161+12,'161');
text(0.73+1+0.46,99+12,'99');

grid on;%添加网格线
%ch = get(b,'children');
title('实验2')
set(gca,'XTickLabel',{ 
   'LINEITEM','ORDERS'})%设置X轴显示

%set(ch,'FaceVertexCData',[0 0 1;1 0 0;0 1 0])
legend('分区1','分区2','分区2');%设置
ylabel('时间(秒)');%设置y轴名字
applyhatch(gcf,'/\xx');%对原图添加填充,这里有个bug就是xx需要输入两次

显示结果:
在这里插入图片描述
填充后效果:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/180434.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java面试题目,Java中级面试题及答案整理(1)

    Java面试题目,Java中级面试题及答案整理(1)(5)GlobalSession:这个只在portal应用中有用,给每一个globalhttpsession新建一个Bean实例。5、Spring事务传播行为所谓事务的传播行为是指,如果在开始当前事务之前,一个事务上下文已经存在,此时有若干选项可以指定一个事务性方法的执行行为。在TransactionDefinition定义中包括了如下几个表示传播行为的常量:TransactionDefinition.PROPAGATION_REQUIRED:如果当前存在事务,则加入该事务;如果当前没有

    2022年8月31日
    4
  • pytest重试_pycharm run运行不了

    pytest重试_pycharm run运行不了安装:pip3installpytest-rerunfailures重新运行所有失败用例要重新运行所有测试失败的用例,请使用–reruns命令行选项,并指定要运行测试的最大次数:$py

    2022年7月31日
    9
  • vdbench数据校验翻译

    vdbench数据校验翻译本文翻译自vdbench的使用手册中的数据校验章节,如有纰漏,还请不吝赐教。vdbench源码下载地址:https://www.oracle.com/downloads/server-storage/vdbench-source-downloads.html数据校验在性能测试的时候不应该被使用,处理器开销可能影响性能测试的结果。在我开始之前,我想问一个想了很多次的问题:“为什么我使用vdbench去检查数据冲突?我也可以写一个大文件,计算校验和,然后重新读这个文件并比较校验和。”当然,你可.

    2022年5月12日
    52
  • Oracle触发器和事务[通俗易懂]

    Oracle触发器和事务[通俗易懂] oracle触发器和事务2015年11月24日14:16:43 it_taojingzhan 阅读数:320编写触发器时,需要注意以下几点:l        触发器不接受参数。l        一个表上最多可有12个触发器,但同一时间、同一事件、同一类型的触发器只能有一个。并各触发器之间不能有矛盾。l        在一个表上的触发器越多,对在该表上的DML操作的性能影…

    2022年7月27日
    11
  • 树莓派搭建MQTT服务器(基于EMQ)「建议收藏」

    树莓派搭建MQTT服务器(基于EMQ)「建议收藏」文章目录1.准备工作1.1知识储备2.安装EMQ3.测试MQTT服务器3.1新建服务器管理员3.2登录到服务器后台3.3MQTT客户端测试1.准备工作1.1知识储备关于MQTT协议能点进来的基本都知道MQTT协议是啥了吧,不知道的自行百度吧,这里就默认各位都知道了。关于EMQEMQX是一款完全开源,高度可伸缩,高可用的分布式MQTT消息服务器,适用于IoT、M2M和移动应用程序,可处理千万级别的并发客户端。EMQX是跨平台的,支持Linux、Unix、macOS以

    2022年5月28日
    150
  • 通过select 和状态EINPROGRESS 实现socket 连接超时判断

    通过select 和状态EINPROGRESS 实现socket 连接超时判断调用connect连接一般的超时时间是75s,但是在程序中我们一般不希望等这么长时间采取采取动作。可以在调用connect之前设置套接字非阻塞,然后调用connect,此时connect会立刻返回,如果连接成功则直接返回0(成功),如果没有连接成功,也会立即返回并且会设置errno为EINPROCESS,这并不是一个致命错误,仅仅是告知你已经在连接了,你只要判断是它就继续执行后面的逻辑就行

    2022年7月17日
    17

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号