MATLAB 绘图笔记——绘制两端尖角colorbar
简介
有时候在绘制colorbar时,当设置了数据的取值范围后,超出范围两端的表示方法有时候需要增加尖角进行描述。而Matlab自带colorbar函数并没有该功能。可以通过增加功能函数使得colorbar增加尖角。
方法1
代码源自Matlab论坛:https://www.mathworks.com/matlabcentral/fileexchange/52515-cbarrow-pointy-ends-for-colorbars
函数代码
function h = cbarrow(options)
%% cbarrow documentation
% The cbarrow function places triangle-shaped endmembers on colorbars to
% indicate that data values exist beyond the extents of the values shown in
% the colorbar.
%
% This function works by creating a set of axes atop the current figure and
% placing patch objects on the new axes. Thus, editing a figure after
% calling cbarrow may cause some glitches. Therefore, it is recommended to call
% cbarrow last when creating plots.
%
%% Syntax
%
% cbarrow
% cbarrow(Direction)
% cbarrow('delete')
% h = cbarrow(...)
%
%% Description
%
% cbarrow places triangle-shaped endmembers on both ends of the current
% colorbar.
%
% cbarrow(Direction) specifies a single direction to place a colorbar end
% arrow. Direction can be 'up', 'down', 'right', or 'left'.
%
% cbarrow('delete') deletes previously-created cbarrow objects.
%
% h = cbarrow(...) returns a handle of the axes on which cbarrow
% objects are created.
%
%% Example 1: Both directions
%
% surf(peaks)
% axis tight
% colorbar
% caxis([0 3])
% cbarrow
%
%% Example 2: One direction
%
% surf(peaks)
% axis tight
% colorbar('southoutside')
% colormap(brewermap(256,'*RdBu'))
% caxis([-7 7])
% cbarrow('right')
%
%% Known issues
% This function only works once per figure. If you have multiple subplots,
% you can only use it once, and you'll have to call cbarrow last. Also,
% editing plots after calling cbarrow can sometimes be a bit glitchy.
%
%% Author Info
% The newcolorbar function was written by Chad A. Greene of the
% University of Texas at Austin's Institute for Geophysics (UTIG), August 2015.
% Updated June 2016 to fix a bug in cbarrow('down'), thanks to Aodat for pointing this out.
% http://www.chadagreene.com.
%
% See also caxis and colorbar.
%% Error checks:
assert(verLessThan('matlab','8.4.0')==0,'Sorry, the cbarrow function requires Matlab R2014b or later.')
narginchk(0,1)
%% Guess which arrows to create based on current colorbar orientation:
% Find handles of all colorbars in current figure:
hcb = findobj(gcf,'Type','Colorbar');
% If no colorbars exist in current figure, create a new one:
if isempty(hcb)cb = colorbar;
else% Otherwise, use the most recent colorbar in the list: cb = hcb(1);
endcbpos = cb.Position;
ax1 = gca;
ax1pos = get(ax1,'OuterPosition');
% If the colorbar is wider than it is tall, make left and right arrows:
if cbpos(4)<cbpos(3)makerightarrow = true; makeleftarrow = true; makeuparrow = false; makedownarrow = false;
else% Otherwise make up and down arrows: makerightarrow = false; makeleftarrow = false; makeuparrow = true; makedownarrow = true;
end
%% Override automatic arrow selection if user requested a specific arrow:
if nargin>0 switch lower(options)case {'del','delete'}tryh_cbarrow = findobj(gcf,'tag','cbarrow'); delete(h_cbarrow); endreturncase {'r','right'}makerightarrow = true; makeleftarrow = false; case {'l','left'}makeleftarrow = true; makerightarrow = false; case {'u','up'} makeuparrow = true; makedownarrow = false; case {'d','down'} makedownarrow = true; makeuparrow = false; otherwiseerror('Invalid input in cbarrow. Must be ''up'',''down'', ''left'', ''right'', ''delete'', or no inputs at all for automatic cbarrowing.') end
end
%% Shrink position of the colorbar to allow room for arrows:
if makerightarrowcbpos = cbpos + [0 0 -cbpos(4)*sqrt(2)/2 0];cb.Position = cbpos;
end
if makeleftarrowcbpos = cbpos + [cbpos(4)*sqrt(2)/2 0 -cbpos(4)*sqrt(2)/2 0];cb.Position = cbpos;
end
if makeuparrow cbpos = cbpos + [0 0 0 -cbpos(3)*sqrt(2)/2];cb.Position = cbpos;
end
if makedownarrow cbpos = cbpos + [0 cbpos(3)*sqrt(2)/2 0 -cbpos(3)*sqrt(2)/2];cb.Position = cbpos;
end
%% Create triangle arrows as patch objects in new axes:
% Get colormap so we know what color to make the triangles:
cm = colormap;
% Create background axes on which to plot patch objects:
h = axes('position',[0 0 1 1],'tag','cbarrow');
hold on
% Plot arrows:
if makerightarrow rightarrowx = (cbpos(1)+cbpos(3)) + [0 cbpos(4)*sqrt(2)/2 0 0]; rightarrowy = cbpos(2) + [0 cbpos(4)/2 cbpos(4) 0]; hr = patch(rightarrowx,rightarrowy,cm(end,:),'EdgeColor',cm(end,:));
end
if makeleftarrowleftarrowx = cbpos(1) + [0 -cbpos(4)*sqrt(2)/2 0 0]; leftarrowy = cbpos(2) + [0 cbpos(4)/2 cbpos(4) 0]; hl = patch(leftarrowx,leftarrowy,cm(1,:),'EdgeColor',cm(1,:));
end
if makeuparrowuparrowx = cbpos(1) + [0 cbpos(3) cbpos(3)/2 0]; uparrowy = cbpos(2)+cbpos(4) + [0 0 cbpos(3)*sqrt(2)/2 0]; hu = patch(uparrowx,uparrowy,cm(end,:),'EdgeColor',cm(end,:));
end
if makedownarrowdownarrowx = cbpos(1) + [0 cbpos(3) cbpos(3)/2 0]; downarrowy = cbpos(2) + [0 0 -cbpos(3)*sqrt(2)/2 0]; hd = patch(downarrowx,downarrowy,cm(1,:),'EdgeColor',cm(1,:));
end
%% Change edge colors:
if strcmpi(cb.Box,'on')% Get starting color and linewidth of colorbar box: linecolor = cb.Color; linewidth = cb.LineWidth; % Turn off colorbar box and we'll create a new one: cb.Box = 'off'; % Edge line for left and right arrows: if all([makerightarrow makeleftarrow])line(cbpos(1) +[0 cbpos(3) cbpos(3)+cbpos(4)*sqrt(2)/2 cbpos(3) 0 -cbpos(4)*sqrt(2)/2 0],...cbpos(2) + [0 0 cbpos(4)/2 cbpos(4) cbpos(4) cbpos(4)/2 0],...'color',linecolor,'LineWidth',linewidth)end% Edge line for right only: if all([makerightarrow ~makeleftarrow])line(cbpos(1) +[0 cbpos(3) cbpos(3)+cbpos(4)*sqrt(2)/2 cbpos(3) 0 0],...cbpos(2) + [0 0 cbpos(4)/2 cbpos(4) cbpos(4) 0],...'color',linecolor,'LineWidth',linewidth)end% Edge line for left arrow only: if all([~makerightarrow makeleftarrow])line(cbpos(1) +[0 cbpos(3) cbpos(3) 0 -cbpos(4)*sqrt(2)/2 0],...cbpos(2) + [0 0 cbpos(4) cbpos(4) cbpos(4)/2 0],...'color',linecolor,'LineWidth',linewidth)end% Edge line for up and down arrows: if all([makeuparrow makedownarrow])line(cbpos(1) +[0 0 cbpos(3)/2 cbpos(3) cbpos(3) cbpos(3)/2 0],...cbpos(2) + [0 cbpos(4) cbpos(4)+cbpos(3)*sqrt(2)/2 cbpos(4) 0 -cbpos(3)*sqrt(2)/2 0],...'color',linecolor,'LineWidth',linewidth)end% Edge line for up arrow only: if all([makeuparrow ~makedownarrow])line(cbpos(1) +[0 0 cbpos(3)/2 cbpos(3) cbpos(3) 0],...cbpos(2) + [0 cbpos(4) cbpos(4)+cbpos(3)*sqrt(2)/2 cbpos(4) 0 0],...'color',linecolor,'LineWidth',linewidth)end% Edge line for down arrow only: if all([~makeuparrow makedownarrow])line(cbpos(1) +[0 0 cbpos(3) cbpos(3) cbpos(3)/2 0],...cbpos(2) + [0 cbpos(4) cbpos(4) 0 -cbpos(3)*sqrt(2)/2 0],...'color',linecolor,'LineWidth',linewidth)end
end
%% Clean up:
axis off
axis([0 1 0 1])
% If original current axes were resized, unresize them:
set(ax1,'OuterPosition',ax1pos)
switch lower(cb.Location) case {'south','east','north','west'} % Bring our arrow patch object axes to the front if the colorbar is inside the current axes: axes(h)otherwise% Bring user's data axes to the front: axes(ax1)
end
% Delete output if user did not request it:
if nargout==0clear h
end
end
用法
surf(peaks)
axis tight
colorbar
caxis([0 3])
cbarrow
效果
方法2
需要安装m_map工具箱。该方法需要设置colorbar的位置,这一点不是很好把握,但是做出来的colorbar效果还是很不错的,还可以设置非线性标签。图列中的数据需要自行准备。
代码
clf;
m_proj('miller','lat',82);
m_pcolor(m_lon,m_lat,m_sps);
[CS,CH]=m_contourf(m_lon,m_lat,m_sps,[0.1:0.1:0.5 1:0.5:1.5],'edgecolor','none');m_coast('color',[0 0 0], 'linewidth', 1.2);
m_grid('tickdir','out','linewi',2);ax = m_contfbar([.3 .7],.05,CS,CH);% 如果bar是竖直的,这里是ytick和yticklabel
ax.XTick = [0.1 0.5 1:0.5:1.5];
ax.XTickLabel = [0.1 0.5 1:0.5:1.5];
colormap(flipud(m_colmap('Blues')))
% close all;
MATLAB 绘图笔记——绘制两端尖角colorbar相关推荐
- matlab绘图笔记
matlab绘图 原文出处:https://ww2.mathworks.cn/help/ 基本绘图函数 创建绘图 plot plot 函数具有不同的形式,具体取决于输入参数. 如果 y 是向量,plo ...
- html矩形加三角,html如何绘制带尖角(三角)的矩形
结合实际情况自己写的: .menu_triangle { height: 10px; width: 10px; background-color: #049888; transform: transl ...
- MATLAB绘图笔记——画圆的几种方法
列举了三种画圆的方法,推荐使用方法3. 方法1: filledCircle([1,3],3,100,'k'); hold on filledCircle([-0.25,4.25],1,100,'r') ...
- matlab绘图实例-绘制双纵轴曲线图
clc clearx = linspace(0,1,10); y1 = x; y2 = -1e5*(x-1);figure; [Axess, Line1, Line2] = plotyy(x,y1, ...
- Matlab绘图:绘制双坐标轴(双y轴)并设置两个y轴的颜色
改变颜色有两种方法: 第一种 在开头就设置 close all fig = figure; left_color = [0 0 1]; right_color = [0 1 0]; set(fig,' ...
- MATLAB绘图笔记——画立方体的几种方法
https://blog.csdn.net/wokaowokaowokao12345/article/details/79519903
- MATLAB绘图_1绘制衰减震荡曲线
例1.1 用M文件画出衰减震荡曲线 y=e−t3cos3ty = e ^ { - \frac { t } { 3 } } \cos 3 ty=e−3tcos3t 及它的包络线 y0=e−t3{ y ...
- B站台湾大学郭彦甫|MATLAB 学习笔记|06 高阶绘图 Advanced Plot
MATLAB学习笔记(06 高阶绘图 Advanced Plot) 如果想获得更好浏览体验的朋友可以转到下面链接 06 1. 对数图 (Logarithm Plots) x = logspace(-1 ...
- MATLAB学习笔记——二维和三维绘图
MATLAB学习笔记--二维和三维绘图 近期练习matlab的二维和三维绘图,整理一下,以防忘记. 文章目录 MATLAB学习笔记--二维和三维绘图 一.二维绘图 1.plot命令 2.fplot 命 ...
- MATLAB学习笔记5:绘图基础与数据可视化(中)
阅读前请注意: 1. 该学习笔记是华中师范大学HelloWorld程序设计协会2021年寒假MATLAB培训的学习记录,是基于培训课堂内容的总结归纳.拓展阅读.博客内容由 @K2SO4钾 撰写.编辑, ...
最新文章
- 人工智能的爆点来临,这些金科玉律先得掌握
- python 虚拟环境 virtualenv virtualenvwrapper的使用方法、命令
- 为什么要娶就娶电力女?!
- 软件或jar包等名字里的GA意思
- 【Java】二十一点小游戏
- 统计学习方法 李航 读书笔记
- linux7 vi 末行 快捷键,vi 常用操作快捷键
- Linux内核移植入门
- ILSpy的下载与使用
- 硅谷谍战: Menlo Park某VC是CIA开的, 你们公司实习生可能是科技间谍…
- 程序员的炫酷动态科幻桌面壁纸
- python实现爬虫收集图片 花瓣网_利用Python抓取花瓣网美图实例
- 高通驱动程序开发参考(一)
- 5G/NR LTE: 物理层抽象 PHY abstraction - SLS LLS 系统级仿真和链路级仿真
- 年底裁员潮,这个冬天你怎样度过?
- 数据挖掘实战:二手车交易价格预测
- 《军师联盟》把三国带跑偏 是时候温习下这五部剧了
- java 变量共享_Java并发编程之共享变量
- Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/spark/sql/SparkSession
- 原始投资100美元,C语言实现单利息投资和复合利息投资