Add hatch to bar plot

  1 function applyhatch(h,patterns,colorlist)
  2 %APPLYHATCH Apply hatched patterns to a figure
  3 %  APPLYHATCH(H,PATTERNS) creates a new figure from the figure H by
  4 %  replacing distinct colors in H with the black and white
  5 %  patterns in PATTERNS. The format for PATTERNS can be
  6 %    a string of the characters ‘/‘, ‘\‘, ‘|‘, ‘-‘, ‘+‘, ‘x‘, ‘.‘
  7 %    a cell array of matrices of zeros (white) and ones (black)
  8 %
  9 %  APPLYHATCH(H,PATTERNS,COLORS) maps the colors in the n by 3
 10 %  matrix COLORS to PATTERNS. Each row of COLORS specifies an RGB
 11 %  color value.
 12 %
 13 %  Note this function makes a bitmap image of H and so is limited
 14 %  to low-resolution, bitmap output.
 15 %
 16 %  Example 1:
 17 %    bar(rand(3,4));
 18 %    applyhatch(gcf,‘\-x.‘);
 19 %
 20 %  Example 2:
 21 %    colormap(cool(6));
 22 %    pie(rand(6,1));
 23 %    legend(‘Jan‘,‘Feb‘,‘Mar‘,‘Apr‘,‘May‘,‘Jun‘);
 24 %    applyhatch(gcf,‘|-+.\/‘,cool(6));
 25 %
 26 %  See also: MAKEHATCH
 27 %  By Ben Hinkle, [email protected]
 28 %  This code is in the public domain.
 29
 30 oldppmode = get(h,‘paperpositionmode‘);
 31 oldunits = get(h,‘units‘);
 32 set(h,‘paperpositionmode‘,‘auto‘);
 33 set(h,‘units‘,‘pixels‘);
 34 figsize = get(h,‘position‘);
 35 if nargin == 2
 36   colorlist = [];
 37 end
 38 bits = hardcopy(h,‘-dzbuffer‘,‘-r0‘);
 39 set(h,‘paperpositionmode‘,oldppmode);
 40 bwidth = size(bits,2);
 41 bheight = size(bits,1);
 42 bsize = bwidth * bheight;
 43 if ~isempty(colorlist)
 44   colorlist = uint8(255*colorlist);
 45   [colors,colori] = nextnonbw(0,colorlist,bits);
 46 else
 47   colors = (bits(:,:,1) ~= bits(:,:,2)) | ...
 48            (bits(:,:,1) ~= bits(:,:,3));
 49 end
 50 pati = 1;
 51 colorind = find(colors);
 52 while ~isempty(colorind)
 53   colorval(1) = bits(colorind(1));
 54   colorval(2) = bits(colorind(1)+bsize);
 55   colorval(3) = bits(colorind(1)+2*bsize);
 56   if iscell(patterns)
 57     pattern = patterns{pati};
 58   elseif isa(patterns,‘char‘)
 59     pattern = makehatch(patterns(pati));
 60   else
 61     pattern = patterns;
 62   end
 63   pattern = uint8(255*(1-pattern));
 64   pheight = size(pattern,2);
 65   pwidth = size(pattern,1);
 66   ratioh = ceil(bheight/pheight);
 67   ratiow = ceil(bwidth/pwidth);
 68   bigpattern = repmat(pattern,[ratioh ratiow]);
 69   if ratioh*pheight > bheight
 70     bigpattern(bheight+1:end,:) = [];
 71   end
 72   if ratiow*pwidth > bwidth
 73     bigpattern(:,bwidth+1:end) = [];
 74   end
 75   bigpattern = repmat(bigpattern,[1 1 3]);
 76   color = (bits(:,:,1) == colorval(1)) & ...
 77           (bits(:,:,2) == colorval(2)) & ...
 78           (bits(:,:,3) == colorval(3));
 79   color = repmat(color,[1 1 3]);
 80   bits(color) = bigpattern(color);
 81   if ~isempty(colorlist)
 82     [colors,colori] = nextnonbw(colori,colorlist,bits);
 83   else
 84     colors = (bits(:,:,1) ~= bits(:,:,2)) | ...
 85              (bits(:,:,1) ~= bits(:,:,3));
 86   end
 87   colorind = find(colors);
 88   pati = (pati + 1);
 89   if pati > length(patterns)
 90     pati = 1;
 91   end
 92 end
 93 newfig = figure(‘units‘,‘pixels‘,‘visible‘,‘off‘);
 94 imaxes = axes(‘parent‘,newfig,‘units‘,‘pixels‘);
 95 im = image(bits,‘parent‘,imaxes);
 96 fpos = get(newfig,‘position‘);
 97 set(newfig,‘position‘,[fpos(1:2) figsize(3) figsize(4)+1]);
 98 set(imaxes,‘position‘,[0 0 figsize(3) figsize(4)+1],‘visible‘,‘off‘);
 99 set(newfig,‘visible‘,‘on‘);
100 function [colors,out] = nextnonbw(ind,colorlist,bits)
101 out = ind+1;
102 colors = [];
103 while out <= size(colorlist,1)
104   if isequal(colorlist(out,:),[255 255 255]) | ...
105         isequal(colorlist(out,:),[0 0 0])
106     out = out+1;
107   else
108     colors = (colorlist(out,1) == bits(:,:,1)) & ...
109              (colorlist(out,2) == bits(:,:,2)) & ...
110              (colorlist(out,3) == bits(:,:,3));
111     return
112   end
113 end
114 %而applyhatch函数需要调用下面的函数
115 function A = makehatch(hatch)
116 %MAKEHATCH Predefined hatch patterns
117 %  MAKEHATCH(HATCH) returns a matrix with the hatch pattern for HATCH
118 %   according to the following table:
119 %      HATCH        pattern
120 %     -------      ---------
121 %        /          right-slanted lines
122 %        \          left-slanted lines
123 %        |          vertical lines
124 %        -          horizontal lines
125 %        +          crossing vertical and horizontal lines
126 %        x          criss-crossing lines
127 %        .          single dots
128 %
129 %  See also: APPLYHATCH
130 %  By Ben Hinkle, [email protected]
131 %  This code is in the public domain.
132 n = 6;
133 A=zeros(n);
134 switch (hatch)
135 case ‘/‘
136   A = fliplr(eye(n));
137 case ‘\‘
138   A = eye(n);
139 case ‘|‘
140   A(:,1) = 1;
141 case ‘-‘
142   A(1,:) = 1;
143 case ‘+‘
144   A(:,1) = 1;
145   A(1,:) = 1;
146 case ‘x‘
147   A = eye(n) | fliplr(diag(ones(n-1,1),-1));
148 case ‘.‘
149   A(1:2,1:2)=1;
150 otherwise
151   error([‘Undefined hatch pattern "‘ hatch ‘".‘]);
152 end
153
154
155 %测试的例子命令
156 %  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]
157 %  bar(data,1)
158 %  axis([0 6 0.0 100])
159 %  legend(‘方法‘,‘exited‘,‘Square‘)
160 %  set(gca,‘XTickLabel‘,{‘Img1‘,‘Img2‘,‘Img3‘,‘Img4‘})
161 %  applyhatch(gcf,‘\.x.‘)

---恢复内容结束---

时间: 2024-08-07 10:28:18

Add hatch to bar plot的相关文章

pandas.DataFrame.plot

pandas.DataFrame.plot¶ DataFrame.plot(x=None, y=None, kind='line', ax=None, subplots=False, sharex=None, sharey=False, layout=None, figsize=None, use_index=True, title=None, grid=None, legend=True, style=None, logx=False, logy=False, loglog=False, xt

Android设计和开发系列第二篇:Action Bar(Develop—Training)

Adding the Action Bar GET STARTED DEPENDENCIES AND PREREQUISITES Android 2.1 or higher YOU SHOULD ALSO READ Action Bar Implementing Effective Navigation DESIGN GUIDE Action Bar The action bar is one of the most important design elements you can imple

Android设计和开发系列第二篇:Action Bar(Develop—API Guides)

Action Bar IN THIS DOCUMENT Adding the Action Bar Removing the action bar Using a logo instead of an icon Adding Action Items Handling clicks on action items Using split action bar Navigating Up with the App Icon Adding an Action View Handling collap

Status Bar in iOS7

This is a very important change in iOS 7: the status bar is no longer a separate bar. It’s now something that simply gets drawn on top of your view controllers. In previous versions of iOS, if your app displayed the status bar the height of the view

共用y轴的双图形绘制

实现这种形式的图形,可通过matplotlib和pandas的实现,相比下pandas实现方便的多. 我数据分析的时候主要是stacked bar.bar和line形式的放在一张图上.stacked bar若用matplotlib实现的话会比较复杂(多组) 先上图吧 def plot_stacked_bar(left_data, right_data): width = .3 axe = plt.subplot(111) axe = left_data.plot(kind='bar', stac

Pandas Api 不完全翻译

原文地址 http://pandas.pydata.org/pandas-docs/stable/api.html API Reference Input/Output Pickling read_pickle(path) Load pickled pandas object (or any other pickled object) from the specified Flat File read_table(filepath_or_buffer[, sep, ...]) Read gene

ggplot2-为图形加入直线

本文更新地址:http://blog.csdn.net/tanzuozhev/article/details/51112057 本文在 http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/ 的基础上加入了自己的理解 对于连续型数据轴和离散型数据轴 # Some sample data dat <- read.table(header=TRUE, text=' cond result control 10 treatment 11.5 '

LaTeX绘图宏包 Pgfplots package

Pgfplots package The pgfplots package is a powerful tool, based on tikz, dedicated to create scientific graphs. Contents 1 Introduction 2 The document preamble 3 2D plots 3.1 Plotting mathematical expressions 3.2 Plotting from data 3.3 Scatter plots

PHP图标类库 - JpGraph使用详解

http://w3note.com/web/181.html 微信平台开发的推广支持应用里,为了满足用户渠道推广分析的需要,公众平台提供了生成带参数二维码的接口.使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送,借此可以通过统计不同场景扫描的二维码的结果来获取商业信息. 为了更形象地展示统计结果,可以使用php作图,不过这需要掌握复杂抽象的画图函数,这里推荐使用php的JpGraph图表类库,它使得作图变成了一件非常简单的事情,你只需从数据库中取出相关数据,定义标