今天想把matlab一段画图代码转为python,使用到了colormap,尝试了一下午,将结果以及一些注意事项记录下来,算作学习笔记了。
先上matlab原代码
a = 0.13; b = 0.12;
c = 0.7; d = 0.8;
figure( 'Position', [200 50 1100 900], 'InvertHardcopy', 'off', 'Color', 'w' );
colormap( jet(512) );
han = axes( 'Units', 'normalized', 'Position', [a b c d],...
'Xlim', [timee(1), timee(timedim)],...
'YLim', [-30, 20], 'Ydir', 'reverse',...
'FontName', 'Times', 'FontSize', 24, 'FontWeight', 'bold', 'Layer', 'top','LineWidth',1.5);
hold on;
xlabel( 'Time (ps)', 'FontName', 'Times', 'FontWeight', 'bold', 'FontSize', 32);
ylabel( 'Position (nm)', 'FontName', 'Times', 'FontWeight', 'bold', 'FontSize', 32);
text( 0.10, 0.90, sprintf( '%5.0f ps', 10), 'Units', 'normalized','HorizontalAlignment', 'center', 'FontName', 'Times', 'FontSize', 50, 'FontWeight', 'bold', 'Color', 'r' );
set(colorbar, 'Position', [0.89 0.12 0.02 0.78], 'FontName', 'Times',...
'FontSize', 24, 'FontWeight', 'bold' );
caxis([300 9500])
% 'Dirction', 'reverse'
hold on;
pcolor(timee, pos, Data');
shading interp;
hold on;
text( 1.04, 0.5, 'Temperature (K)', 'Units', 'normalized', 'Rotation', 90, 'HorizontalAlignment', 'center', ...
'FontName', 'Times', 'FontSize', 32, 'FontWeight', 'bold', 'Color', 'k' );
结果如下:
fig, ax = plt.subplots(figsize=(15, 10))
c = ax.pcolormesh(timee, pos, Data.T, cmap='jet', vmin=1000, vmax=9000, shading='gouraud')
ax.set_xlim(timee[0], timee[timedim-1])
ax.set_ylim(-30, 20)
c_ = fig.colorbar(c, ax=ax)
c_.ax.tick_params(labelsize=32) # 设置colorbar的标签字体及其大小
ax.set_xticklabels(ax.get_xticks(), size=30)
ax.set_yticklabels(ax.get_yticks(), size=30)
ax.set_xlabel('Time (ps)', family='Times', weight='bold', fontsize=32)
ax.set_ylabel('Position (nm)', family='Times', weight='bold', fontsize=32)
ax.invert_yaxis()
ax.text(51.5, 4.5, 'Temperature (K)', rotation=90, horizontalalignment='center',
family='Times', fontsize=25, weight='bold', color='k')
ax.text(7, -25, '10 ps', horizontalalignment='center',
family='Times', fontsize=50, weight='bold', color='r')
plt.show()
注意:
在pcolormesh函数使用过后,就必须要设置xlim或者ylim,然后再使用colorbar的函数,否则设置ylim会导致语句无效,不会报错但是改不了。想让图像更平滑一些,就加这个参数shading='gouraud'
可以参考下面的网站,熟悉axes的用法。
axes相关属性设置
pcolormesh的参数
Parameters: | C : array_like A scalar 2-D array. The values will be color-mapped. X, Y : array_like, optional cmap : str or Colormap, optional A Colormap instance or registered colormap name. The colormap maps the C values to colors. Defaults to norm : Normalize, optional The Normalize instance scales the data values to the canonical colormap range [0, 1] for mapping to colors. By default, the data range is mapped to the colorbar range using linear scaling. Normalize实例将数据值缩放到用于映射到颜色的规范化colormap范围[0,1]。默认情况下,使用线性缩放将数据范围映射到colorbar范围。 vmin, vmax : scalar, optional, default: None The colorbar range. If None, suitable min/max values are automatically chosen by the Normalizeinstance (defaults to the respective min/max values of C in case of the default linear scaling). edgecolors : {'none', None, 'face', color, color sequence}, optional 边缘颜色,默认None:
The singular form edgecolor works as an alias. alpha : scalar, optional, default: None 透明度 shading : {'flat', 'gouraud'}, optional The fill style, Possible values: 填充式样
snap : bool, optional, default: False文章来源:https://www.toymoban.com/news/detail-456005.html Whether to snap the mesh to pixel boundaries文章来源地址https://www.toymoban.com/news/detail-456005.html |
---|
到了这里,关于Matlab转Python 画图pcolormesh的使用及一些注意事项的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!