简体   繁体   English

分别保存颜色条以用于散点图

[英]Save colorbar for scatter plot separately

I've got scatter plot with colorbar which I save as PNG image. 我有带有colorbar的散点图,我将其另存为PNG图像。 I need the plot to be of a certain figsize but adding colorbar scales original plot. 我需要该图具有一定的figsize,但要添加颜色条缩放原始图。

import pylab as plt

plt.figure(figsize=FIGSIZE)
plt.scatter(X, Y, c=Z, s=marker_size, norm=LogNorm(), vmin=VMIN, vmax=VMAX, cmap=CMAP,rasterized=True,lw=0,)
CB = plt.colorbar(ticks=TICKS, format=FORMAT)

How could I save original plot (with figsize set as above) and colorbar as two separate images? 如何将原始图(按上述设置figsize)和颜色栏保存为两个单独的图像?

The obvious answer is "plot your colorbar separately". 显而易见的答案是“分别绘制颜色栏”。 You need to create a new figure window and plot your colorbar there, in order to prevent your first figure from being distorted. 您需要创建一个新的图形窗口并在其中绘制颜色栏,以防止您的第一个图形变形。 Small example: 小例子:

import matplotlib.pyplot as plt
import numpy as np    # only for dummy data

X,Y = np.mgrid[-2:3,-2:3]
Z = np.random.rand(*X.shape)
FIGSIZE = (2,3)

plt.figure(figsize=FIGSIZE)
mpb = plt.pcolormesh(X,Y,Z,cmap='viridis')
# plot the original without a colorbar
plt.savefig('plot_nocbar.png')

# plot a colorbar into the original to see distortion
plt.colorbar()
plt.savefig('plot_withcbar.png')

# draw a new figure and replot the colorbar there
fig,ax = plt.subplots(figsize=FIGSIZE)
plt.colorbar(mpb,ax=ax)
ax.remove()
plt.savefig('plot_onlycbar.png')

# save the same figure with some approximate autocropping
plt.savefig('plot_onlycbar_tight.png',bbox_inches='tight')

Consider the following four figures that were produced (click to view properly): 考虑生成的以下四个图(单击以正确查看):

没有颜色条 色彩条失真 带有许多白色的单独色条 裁剪色条

The first is a saved version of the figure without a call to colormap . 第一个是该图的保存版本,未调用colormap This is fine, this is what you want to preserve. 很好,这就是您要保留的内容。 The second figure shows what happens if we call colorbar without any extra fuss: it takes some space from the original figure, and this is what you want to prevent. 第二colorbar图显示了如果我们在不加任何额外大惊小怪的情况下调用colorbar会发生什么:它colorbar原始图占用一些空间,这就是您要防止的。

You have to open a new figure (and axes) using plt.subplots , with the size of your original figure. 您必须使用plt.subplots打开一个新图形(和轴),其大小为原始图形的大小。 This way you can be sure that the produced colorbar will be the same size as if it was drawn in your original figure. 这样,您可以确保产生的颜色条的大小与原始图形中绘制的颜色条相同。 In the above setup I let matplotlib determine the size of the colorbar itself; 在上面的设置中,我让matplotlib确定颜色条本身的大小。 but then afterward we need to delete the auxiliary axes that would pollute the resulting plot. 但是之后,我们需要删除会污染结果图的辅助轴。 (The other option would be to create a single axes in the new figure manually, with the expected size of the colorbar. I suspect this is not a feasible course of action.) (另一种选择是在新图形中手动创建单个轴,并使用颜色条的预期大小。我怀疑这不是可行的操作。)

Now, as you can see in the third plot, the empty space left after the deleted axes is clearly visible in the resulting plot (but the size of the colorbar is perfect, correspondingly). 现在,如您在第三幅图中看到的那样,在结果图中可以清楚地看到在删除的轴之后留下的空白空间(但是,相应地,颜色栏的大小是完美的)。 You can either cut this white space off manually in post-production, or use something that autocrops your colorbar image. 您可以在后期制作中手动切断此空白区域,也可以使用可以自动裁剪彩条图像的内容。

I also included a version of the plot wherein matplotlib itself crops most of the figure: the bbox_inches='tight' keyword argument to savefig does exactly this. 我还包括一个版的情节,其中matplotlib本身作物大部分人物组成: bbox_inches='tight'的关键字参数savefig正是这样做的。 The upside is that the resulting image file only contains the colorbar (as seen above in the fourth image), but the size of the resulting colorbar will be slightly different from your original. 好的方面是,生成的图像文件仅包含颜色条(如上面的第四幅图像所示),但是生成的颜色条的大小将与原始颜色条略有不同。 Depending on your specific needs, you'll need to experiment with the available methods to come up with a solution that's most convenient for you. 根据您的特定需求,您将需要尝试可用的方法来提出最适合您的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM