大佬教程收集整理的这篇文章主要介绍了在matplotlib中标准化后修剪颜色条,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已将颜色条与水平箱线图的 x 轴对齐。
如下图所示,x轴上有一片空白区域。
所以我想将 x 轴与颜色条一起修剪,颜色条由 vmin
和 vmax
标准化
知道如何做到这一点吗?
非常感谢!
test_data = [np.random.normal(mean,1,10) for mean in range(400,500,10)]
### set sns style
sns.set_style('white')
### set fig,ax
fig = plt.figure(figsize=(4,2))
ax1 = fig.add_axes([0.10,0.10,1.2,2])
### define color bar
# choose color map
cmap = plt.get_cmap('nipy_spectral')
# normalize min and max
norm = mpl.colors.normalize(vmin=380,vmax=780)
# assign cmap and norm
sm = plt.cm.ScalarMappable(cmap=cmap,norm=norm)
sm.set_array([])
### Plot color bar (change "pad" to adjust distance of color bar to x-axis)
plt.colorbar(sm,ticks=np.linspace(380,780,11),LOCATIOn="bottom",pad=0)
### Plot Boxplot
bplot = ax1.boxplot(test_data,vert=false,patch_artist=TruE)
### Set axis
ax1.set_yticklabels(["a","b","c","d","e","f","g","h","i","j"])
ax1.get_xaxis().set_visible(false)
ax1.set_xlim(380,780)
plt.show()
将颜色条与主 ax 完全对齐的最简单方法是使用与主 ax 相同的 x 和宽度并调整 y 和高度将其 ax 创建为 cax = fig.add_axes([...])
。然后可以将颜色条创建为 plt.colorbar(sm,ticks=np.linspace(380,780,11),orientation='horizontal',cax=caX)
。
由于您想缩小颜色条的一部分,使用将其与 x 轴对齐的范围通过 imshow()
绘制它可能更容易。
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import matplotlib as mpl
test_data = [np.random.normal(mean,1,10) for mean in range(400,500,10)]
sns.set_style('white')
fig = plt.figure(figsize=(10,3))
ax1 = fig.add_axes([0.10,0.17,0.88,0.80]) # x0,y0,width,height in figure coordinates
cax = fig.add_axes([0.10,0.12,0.05],sharex=ax1) # use same x0 and width
### Define color bar
cmap = plt.get_cmap('nipy_spectral')
norm = mpl.colors.Normalize(vmin=380,vmax=780)
xmin,xmax = 380,500
cax.imshow(np.linspace(xmin,xmax,100).reshape(1,-1),extent=[xmin,1],cmap=cmap,norm=norm,aspect='auto')
cax.set_yticks([])
bplot = ax1.boxplot(test_data,vert=false,patch_artist=TruE)
ax1.set_yticklabels(["a","b","c","d","e","f","g","h","i","j"])
ax1.set_xlim(xmin,xmaX)
ax1.tick_params(axis='x',labelbottom=falsE)
cax.set_xticks(np.arange(xmin,xmax + 1,20))
plt.show()
imshow()
也可用于以类似方式为箱线图矩形着色。下面的示例使用改编的示例数据来更好地说明该概念。请注意,imshow
会重置 xlims 和 ylims,因此需要先保存它们,然后再重置。
test_data = [np.random.uniform(mean - 20,mean + 20,510
cax.imshow(np.linspace(xmin,200).reshape(1,patch_artist=True,medianprops={'color': 'white'})
ax1.set_yticklabels(["a","j"])
ax1.tick_params(axis='x',20))
ymin,ymax = ax1.get_ylim()
for art in ax1.artists:
art.set_facecolor('none') # make transparent
x0,x1,y1 = art.get_path().get_extents().extents
ax1.imshow(np.linspace(x0,extent=[x0,y1],aspect='auto',zorder=0)
ax1.set_xlim(xmin,xmaX)
ax1.set_ylim(ymin,ymaX)
以上是大佬教程为你收集整理的在matplotlib中标准化后修剪颜色条全部内容,希望文章能够帮你解决在matplotlib中标准化后修剪颜色条所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。