matlab绘制尖角colorbar

这篇文章详细介绍了如何在Matlab中使用特定颜色映射(如jetcolormap)创建带尖角的颜色条,并配合图形绘制。作者展示了如何设置坐标轴位置、添加颜色刻度以及定制尖角样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Matlab代码

cmap = [69 117 180
    116 173 203
    171 217 233
    254 224 144
    253 174 77
    244 109 67
    215 48 39
    165 0 38]/255;
%画图的部分代码
figure
set(gcf,'outerposition',get(0,'screensize'))
ax = axes('Position',[0.2 0.2 0.6 0.6]); % pos需要自己设置位置
h = colorbar;
% colormap(ax,cmap)
map = colormap(jet);
colorbar_arrow(map,1,get(h,'position'),0.03)
xmin = -15; xmax = 25;
caxis([xmin xmax]);
% % % 绘制带尖角的colorbar
% x y
% 宽度 高度
% 尖角的长度
% 尖角的拐点
x = 0.2; y = 0.05; w = 0.6; h = 0.03;
pos = [x y w h]; % x y 宽度 高度
ax1 = axes('Position',pos);
[c,~] = size(cmap);
cdata = 2:c-1;
imagesc(cdata);
colormap(cmap(2:end-1,:));
box off
ax0 = axes('Position',get(gca,'Position'),'XAxisLocation','top','YAxisLocation','left',...
            'Color','none','XColor','k','YColor','k'); % 将坐标轴的位置换一下,换成上面和右面
set(ax1,'YTick', []);
set(ax0,'YTick', []);
set(ax0,'XTick', []);
box on
set(ax1,'XTick',0.5:c-1.5);
set(ax1,'XTicklabel',(xmin:5:xmax));
set(ax1,'FontSize',12, 'FontName','Arial');
ax1.TickLength = [h+0.005 0];
% 绘制尖角
ax3 = axes('Position',[x-w/(c-2),y,w/(c-2),h],'YAxisLocation','right');
set(ax3,'ycolor','none','xcolor','none');
set(ax3,'XTick',[]);
fc = [1 2 3];
vt = [0 0.5;1 0.04;1 0.96;];
patch('Faces',fc,'Vertices',vt,'FaceColor',cmap(1,:),'EdgeColor',[0 0 0],'linewidth',0.01);
ax4 = axes('Position',[x+w,y,w/(c-2),h],'YAxisLocation','left');
set(ax4,'ycolor','none','xcolor','none');
set(ax4,'XTick',[]);
fc = [1 2 3];
vt = [1 0.5;0 0.96;0 0.04;];
patch('Faces',fc,'Vertices',vt,'FaceColor',cmap(end,:),'EdgeColor',[0 0 0],'linewidth',0.01);


结果展示

在这里插入图片描述

### 使用 R 语言创建带尖角的自定义色条 为了实现这一目标,可以利用 `ggplot2` 和一些辅助包来定制图形元素。下面展示一种方法,在绘图区域外添加一个具有尖角设计的颜色条。 #### 定义函数以生成带尖角的色条 通过组合基本形状(矩形加三角形)并调整它们的位置和大小,可以在 ggplot 图表旁边构建所需的特殊样式颜色条。 ```r library(ggplot2) create_custom_colorbar <- function(colors, width = unit(1,"cm"), height = unit(8,"cm"), tip_length = unit(.5,"cm")) { # 创建基础数据框用于绘制直方部分 rect_df <- data.frame(xmin=c(-width/2), xmax=c(width/2), ymin=c(height - tip_length), ymax=c(tip_length)) # 添加顶部箭头的数据点 (假设向上指向) arrow_points <- rbind( c(-width / 4, height), c(0, height + tip_length), c(width / 4, height) ) list( geom_rect(data=rect_df,aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax), fill="white", color="black") , annotate("polygon", x=arrow_points[,1], y=arrow_points[,2], fill="white", colour="black") ) } ``` 这段代码定义了一个名为 `create_custom_colorbar()` 的新函数[^6],它接受几个参数来控制最终输出的颜色条外观: - `colors`: 颜色序列。 - `width`, `height`: 色条主体宽度和高度。 - `tip_length`: 端长度。 接着,可以通过调用上述函数并将结果附加到现有图表上来显示这个独特的颜色条。 #### 绘制示例图像并与自定义色条结合 这里提供一段完整的脚本作为例子,说明如何将前面提到的功能应用于实际场景中。 ```r # 加载必要的库 if (!requireNamespace("gridExtra", quietly = TRUE)) install.packages('gridExtra') library(gridExtra) # 准备样本数据集 set.seed(123) data_matrix <- matrix(runif(100,min=-1,max=1), nrow=10) # 构建热力图 heatmap_plot <- ggplot(melt(data_matrix), aes(X2,X1,fill=value)) + geom_tile()+ scale_fill_gradient(low="#FDE725FF", high="#440154FF") # 获取配色方案 custom_palette <- rev(RColorBrewer::brewer.pal(n=9,name='RdYlBu')) # 描绘自定义color bar custom_bar_elements <- create_custom_colorbar(custom_palette, width=unit(0.5,'npc'), height=unit(0.8,'npc'), tip_length=unit(0.1,'npc')) # 合并两个组件 final_layout <- grid.arrange(heatmap_plot, arrangeGrob(grobs=custom_bar_elements)) print(final_layout) ``` 此段程序首先准备了一些随机数构成的小型矩阵,并以此为基础制作了一张简单的热力图。之后应用之前编写的 `create_custom_colorbar()` 来获取代表性的颜色条片段,最后借助于 `gridExtra::arrangeGrob()` 把两者拼接在一起形成完整的作品[^7]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值