相关疑难解决方法(0)

pad_inches = 0和bbox_inches ="tight"使得图小于声明的figsize

我正在制作一个嵌入乳胶的出版品质图,我希望在尺寸和字体方面非常精确(因此字体在文章中与图中的尺寸相同).为了防止绘图缩小乳胶,我希望它具有确切的尺寸,但我不能.这是我的代码:

import matplotlib.pyplot as plt
from matplotlib import rc, rcParams
from numpy import sin

rc('text', usetex=True)
rc('font', family='serif', serif='Computer Modern Roman', size=8)
rc('legend', fontsize=10)

width_in = 5
fig = plt.figure(1, figsize=(width_in, 2))
ax = fig.add_subplot(111)
ax.plot(range(0,100), sin(range(0,100)))

fig.tight_layout()
fig.savefig('test.eps', bbox_inches='tight', pad_inches=0)
plt.close()
Run Code Online (Sandbox Code Playgroud)

问题在于bbox_inches ='tight'和pad_inches = 0.添加这些选项使我的绘图宽4.76英寸而不是声明5英寸.但我希望他们节省空间.那怎么解决呢?

编辑:嗯,答案建议删除bbox_inches='tight',pad_inches=0并使用只tight_layout().然后图像大小合适,但它仍然有一些白色的填充物.我可以删除它fig.tight_layout(pad=0),但随后在框内移动的图标题,看起来很难看.另一方面,我可以使用tight_layout(rect=[...])并获得所需的结果,但这是一个手动工作,以使数字正确 - 我不喜欢它.因此,目前我没有看到任何简单而通用的解决方案.

plot matplotlib

22
推荐指数
2
解决办法
4万
查看次数

带有注释的Matplotlib与保存的图形截断

我正在使用matplotlib在使用注释时绘制一些图形.我遇到的问题是注释(在绘图区域之外)与保存的图片截断,如下图所示.

带注释的数字被切断

下图是我想要的:

想要的图片

谁知道如何解决这个问题?我注意到人们建议在rcParams中使用plt.tight_plot()或fig.autolayout,但这似乎不起作用.下面是生成图的代码.

fig, ax = plt.subplots()
ax.set_xlim([-0.02,1.1])
ax.set_ylim([-0.02,1.1])

ax.plot([0,0,0,0.5,0.5,0.5,1,1,1], [0,0.5,1,0,0.5,1,0,0.5,1], 'go')

ax.annotate("Digit 2",
            xy=(0.5, -0.1), xycoords='data',
            xytext=(0.5, -0.3), textcoords='data',
            arrowprops=dict(arrowstyle="->",
                            connectionstyle="arc3"),
             annotation_clip=False,
             fontsize = 12,
             ha='center',
            )

ax.annotate("Level 2",
            xy=(-0.1, 1), xycoords='data',
            xytext=(-0.35, 1), textcoords='data',
            arrowprops=dict(arrowstyle="->",
                            connectionstyle="arc3"),
                    annotation_clip=False,
                    fontsize = 12,
                    va='center',
            )

plt.savefig('sample.png', dpi = 300)
Run Code Online (Sandbox Code Playgroud)

python matplotlib

22
推荐指数
1
解决办法
1万
查看次数

将colorbar放在图中

我有一个简单的散点图,其中每个点都有一个颜色,由0到1之间的值设置为选定的颜色图.这MWE是我的代码:

import matplotlib.pyplot as plt 
import numpy as np
import matplotlib.gridspec as gridspec

x = np.random.randn(60) 
y = np.random.randn(60)
z = [np.random.random() for _ in range(60)]

fig = plt.figure()
gs = gridspec.GridSpec(1, 2)

ax0 = plt.subplot(gs[0, 0])
plt.scatter(x, y, s=20)

ax1 = plt.subplot(gs[0, 1])
cm = plt.cm.get_cmap('RdYlBu_r')
plt.scatter(x, y, s=20 ,c=z, cmap=cm)
cbaxes = fig.add_axes([0.6, 0.12, 0.1, 0.02]) 
plt.colorbar(cax=cbaxes, ticks=[0.,1], orientation='horizontal')

fig.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

PIC

这里的问题是我想要将小的水平颜色条位置放在图的左下方,但使用cax参数不仅感觉有点hacky,它显然与tight_layout导致警告的结果相冲突:

/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py:1533: UserWarning: This figure includes Axes that are not …
Run Code Online (Sandbox Code Playgroud)

python matplotlib color-mapping

10
推荐指数
1
解决办法
6073
查看次数

标签 统计

matplotlib ×3

python ×2

color-mapping ×1

plot ×1