在论文中,图表往往发挥着极为重要的作用,好的图表将能进一步提升论文的质量。在书写论文时,很多时候需要绘制柱状图,然而不同的柱状图如果采用颜色区分,当论文打印以后,视觉效果大打折扣,甚至无法区分。在遇到这个问题时,我通过网站论坛搜索,终于找到了在matlab中绘制柱状图,并采用不同的图案进行表示。主要利用下面的代码。

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:

%      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

%  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

效果如下图所示

转载本文请联系原作者获取授权,同时请注明本文来自时杰科学网博客。

链接地址:http://blog.sciencenet.cn/blog-40165-308439.html

matlab bar 填充花纹,科学网—使用matlab绘画柱状图,且使用不同的图案填充 - 时杰的博文...相关推荐

  1. matlab pdepe函数边界,科学网-使用MATLAB中pdepe函数求解一维偏微分方程-邓浩鑫的博文...

    由于自己科研水平较低,记录的各种体会更多的是给自己做个小结,错误之处,欢迎大家指正. 使用MATLAB求解偏微分方程或者方程组,大致有三类方法.第一种是使用MATLAB中的PDE Toolbox,PD ...

  2. matlab音乐键盘模拟,科学网—[原][Matlab][04] Midi音乐键盘 - 王楠的博文

    以前在网上读到一个用matlab播canon音乐的源码感觉很有意思,但声音还不够好. 于是想做个自己的音乐播放器,有键盘,有对应的钢琴声音,读取乐谱就能播.如下. (1)按键的音高与频率 从1到下一个 ...

  3. matlab排序 第二条件,科学网—【MATLAB技术贴】矩阵多条件排序 - 崔健的博文

    针对矩阵的多个条件排序问题,可以采用如下函数(以列为例):sortrows 具体参数如下: 1.先按照第二列的升序排序,然后按照第一列的升序排序:sortrows(a,[2,1]) 2.先按照第二列的 ...

  4. matlab print 保留颜色,科学网—解决matlab saveas printf保存图片时 colorbar中的字体颜色发生改变 - 肖鑫的博文...

    matlab在画多个图片循环保存时很容易出现看到的图跟保存的图片不一样,最常见的是大小不同导致坐标轴拉伸变形,主要是图片分辨率跟屏幕分辨率不同的原因,这个可以通过在设置figure时,固定画布大小解决 ...

  5. matlab 数组去掉0,科学网—在Matlab环境下去除矩阵中的零向量 - 李航的博文

    主要用到了any()函数. e.g.1 找到矩阵中为零的列向量,并将其删除. >> a=[1,2,3;0,0,0;4,5,6;0,0,0;7,8,9]' a = 1     0     4 ...

  6. matlab mic系数_科学网—最大信息系数 (Maximal Information Coefficient, MIC)详解(1) - 彭勇的博文...

    最大信息系数 (Maximal Information Coefficient, MIC)详解(1) 四年前看过的一篇论文,当时还在组会上报告过,很确信当时把它弄懂了,由于当时是用机器学习的方法来做预 ...

  7. matlab返回每月天数,科学网-[转载] matlab 输入月份得到该月天数-肖鑫的博文

    这个程序是近一年前在百度知道上看到的,发现还挺有用,所以在此分享一下 % 输入201501,返回31 % 输入201502,返回28 % 输入201504,返回30 function [day]=da ...

  8. matlab print 白边,科学网-[原创] matlab输出图片无白边-杨光的博文

    今天要做一个gif动画,可惜GIF Movie Gear不认eps文件,无奈只好输出png格式的文件,麻烦来了,输出的图像有白边!之前挥之不去的问题再一次来了.在网上搜索一个多小时,都是说什么先ims ...

  9. matlab 更换坐标轴_科学网—【Matlab】坐标轴的设置 - 叶瑞杰的博文

    使用matlab的绘图函数plot绘图时系统默认设置了一些属性,例如坐标轴字号大小等并根据情况自动设置坐标轴显示的上下限,这些属性可以通过函数灵活改动,此处总结一小部分. figure; %设置坐标轴 ...

  10. linux fastq.gz文件如何解压,科学网—批量gzip,ungzip快速压缩和解压fastq文件 - 陈明杰的博文...

    二代测序fastq文件太大,解压,压缩很浪费时间. 常规:使用gzip gunzip单线程压缩 进阶1:python调用threading,多线程压缩,相当于开了N个窗口进行解压和压缩,略. 进阶2: ...

最新文章

  1. 无锡初一计算机试题,2015年无锡市初中信息技术考查选择题.doc
  2. IntelliJ IDEA 重大更新:支持CPU火焰图,新增酷炫主题
  3. makefile多文件编译
  4. 不学Python迟早会被淘汰?Python真有这么好的前景?
  5. Qt信号和槽连接方式的选择
  6. 天天象棋 残局闯关 第9关
  7. [Go] 正则表达式 示例
  8. python——rang函数、for、braek循环
  9. 用PHP的CURL写的一个采集Discuz的例子
  10. 蛋壳公寓CEO高靖被限制消费
  11. svn server 配置 与TortoiseSVN、Ankhsvn+VS使用 及 问题
  12. 用Python讲述:地理“经纬度”数据的4种转换方法!
  13. 网站虚拟服务器共享ip与独立ip,误区解读:独立IP与共享IP虚拟主机哪个更好?...
  14. esp8266_deauther将html压缩成字节码
  15. centos7 安装拼音输入法
  16. HTML前端开发入门之表单标签/labei标签/datalist标签
  17. 欠定方程组的最小范数解
  18. NeoCognitron
  19. z自建服务器,《守望先锋》将加入自建服务器 自定规则
  20. 如何制定有效的项目计划,提高团队执行力

热门文章

  1. NFS网络文件系统配置
  2. INSERT … ON DUPLICATE KEY UPDATE
  3. [k8s]容器化node-expolore(9100)+cadvisor(8080)+prometheus(9090) metric搜集,grafana展示
  4. 【BZOJ3379】[Usaco2004 Open]Turning in Homework 交作业 DP
  5. 使用读写锁实现同步数据访问
  6. 有了net send,谁还用IM?
  7. 排序算法--希尔排序
  8. Python:Mysql编程(基础操作命令)
  9. 龙星计划机器学习笔记
  10. c++求两点的距离利用友元_「20525」高中数学:“二面角”和“点到平面的距离”的通解...