Python 删除Matplotlib中楔形极坐标图周围的空间

Python 删除Matplotlib中楔形极坐标图周围的空间,python,matplotlib,plot,Python,Matplotlib,Plot,我开始在Matplotlib中通过设置thetamin和thetamax属性来创建不包含整个圆的极坐标图,即“楔形”图。这是我等待了很长时间的事情,我很高兴他们做到了:) 但是,我注意到,当使用此功能时,图形在轴内的位置似乎以一种奇怪的方式发生变化;根据楔形角孔径的不同,很难对图形进行微调,使其看起来更漂亮 下面是一个例子: import numpy as np import matplotlib.pyplot as plt # get 4 polar axes in a row fig, a

我开始在Matplotlib中通过设置
thetamin
thetamax
属性来创建不包含整个圆的极坐标图,即“楔形”图。这是我等待了很长时间的事情,我很高兴他们做到了:)

但是,我注意到,当使用此功能时,图形在轴内的位置似乎以一种奇怪的方式发生变化;根据楔形角孔径的不同,很难对图形进行微调,使其看起来更漂亮

下面是一个例子:

import numpy as np
import matplotlib.pyplot as plt

# get 4 polar axes in a row
fig, axes = plt.subplots(2, 2, subplot_kw={'projection': 'polar'},
                         figsize=(8, 8))

# set facecolor to better display the boundaries
# (as suggested by ImportanceOfBeingErnest)
fig.set_facecolor('paleturquoise')

for i, theta_max in enumerate([2*np.pi, np.pi, 2*np.pi/3, np.pi/3]):

    # define theta vector with varying end point and some data to plot
    theta = np.linspace(0, theta_max, 181)
    data = (1/6)*np.abs(np.sin(3*theta)/np.sin(theta/2))

    # set 'thetamin' and 'thetamax' according to data
    axes[i//2, i%2].set_thetamin(0)
    axes[i//2, i%2].set_thetamax(theta_max*180/np.pi)

    # actually plot the data, fine tune radius limits and add labels
    axes[i//2, i%2].plot(theta, data)
    axes[i//2, i%2].set_ylim([0, 1])
    axes[i//2, i%2].set_xlabel('Magnitude', fontsize=15)
    axes[i//2, i%2].set_ylabel('Angles', fontsize=15)

fig.set_tight_layout(True)
#fig.savefig('fig.png', facecolor='skyblue')

标签位于不方便的位置,位于刻度标签上方,但可以通过向
set_xlabel
命令添加额外的
labelpad
参数来将标签移近或移远轴,因此这不是什么大问题

不幸的是,我的印象是,该绘图已调整为适合现有的轴尺寸,这反过来会导致在半圆绘图的上方和下方出现非常尴尬的空白(当然,这是我需要使用的)

这听起来应该很容易摆脱——我的意思是,楔形图是自动完成的——但我似乎不知道如何处理半圆。有人能解释一下吗


编辑:抱歉,我的问题不是很清楚;我想创建一个半圆极坐标图,但似乎使用
set_thetamin()
会在图像周围留下大量空白(尤其是上面和下面),如果可能的话,我宁愿删除这些空白

这是一种通常由
tight\u layout()
处理的东西,但在这里它似乎没有起作用。我尝试在绘图后手动更改图形窗口的大小,但空白区域只是随着更改而缩放。下面是一个最低限度的工作示例;如果愿意,我可以让xlabel更接近图像,但保存的图像文件仍然包含大量的空白

有人知道如何删除这个空白吗

import numpy as np
import matplotlib.pyplot as plt

# get a half circle polar plot
fig1, ax1 = plt.subplots(1, 1, subplot_kw={'projection': 'polar'})

# set facecolor to better display the boundaries
# (as suggested by ImportanceOfBeingErnest)
fig1.set_facecolor('skyblue')

theta_min = 0
theta_max = np.pi

theta = np.linspace(theta_min, theta_max, 181)
data = (1/6)*np.abs(np.sin(3*theta)/np.sin(theta/2))

# set 'thetamin' and 'thetamax' according to data
ax1.set_thetamin(0)
ax1.set_thetamax(theta_max*180/np.pi)

# actually plot the data, fine tune radius limits and add labels
ax1.plot(theta, data)
ax1.set_ylim([0, 1])
ax1.set_xlabel('Magnitude', fontsize=15)
ax1.set_ylabel('Angles', fontsize=15)

fig1.set_tight_layout(True)
#fig1.savefig('fig1.png', facecolor='skyblue')


<强>编辑2:< /强>将背景颜色添加到图中,以更好地显示边界,如EngopeTeopeBeNeNest.

< P>中所示,“截断”极轴的楔形似乎放置在原始轴的中间。游戏中似乎有一些结构称为
lockedbox
\u wedgebox
,我以前从未见过,也不完全理解。这些似乎是在抽签时创建的,因此从外部操纵它们似乎介于困难和不可能之间

一种方法是操纵原始轴,使生成的楔块在所需位置向上转动。这不是真正确定的,而是通过反复试验寻找一些好的值

在这种情况下,要调整的参数是图形大小(
figsize
),标签的填充(
labelpad
,如问题中已指出的),最后是轴的位置(
ax.set\u位置([左、下、宽、高])

结果可能是这样的

import numpy as np
import matplotlib.pyplot as plt

# get a half circle polar plot
fig1, ax1 = plt.subplots(1, 1, figsize=(6,3.4), subplot_kw={'projection': 'polar'})
theta_min = 1.e-9
theta_max = np.pi

theta = np.linspace(theta_min, theta_max, 181)
data = (1/6.)*np.abs(np.sin(3*theta)/np.sin(theta/2.))

# set 'thetamin' and 'thetamax' according to data
ax1.set_thetamin(0)
ax1.set_thetamax(theta_max*180./np.pi)

# actually plot the data, fine tune radius limits and add labels
ax1.plot(theta, data)
ax1.set_ylim([0, 1])
ax1.set_xlabel('Magnitude', fontsize=15, labelpad=-60)
ax1.set_ylabel('Angles', fontsize=15)

ax1.set_position( [0.1, -0.45, 0.8, 2])

plt.show()


在这里,我给图形的背景设置了一些颜色,以便更好地看到边界。

@ImportanceOfBeingErnest我认为这是“角度”一词在顶部两个轴中的位置,可能是“大小”一词在右上方轴中的位置,我道歉,我应该说得更清楚一些:我想不出一种方法来消除半圆极坐标图周围的空白,特别是。为了清楚起见,我将对问题进行编辑;谢谢你的反馈。太好了,这正是我想要的:)非常感谢!因此,您的想法是通过将轴的位置设置在图形边界之外来有效地补偿空白,是这样吗?无论如何,它确实起了作用;我可以通过反复试验来微调“模板”,然后用它绘制类似的图形,所以这对我来说不是问题。我也喜欢设置背景色以显示相邻空间的想法;也会编辑问题中的数字!我就这一点发表了一篇文章:虽然这是一个更具愿望的问题,但我不认为这会在不久的将来发生变化;总的来说,我对极坐标图的这种新功能非常满意,所以这个空白的东西在路上是一个相当小的碰撞。如果它在一个中等距离的未来得到排序,那就太好了;如果没有,也可以:)是否可以获得所需的外部坐标,以迭代方式估计
set\u position()
参数?我有一个楔形极坐标图,它与
set_rorigin(0)
和动态轴限制相结合,这会导致绘图尺寸的变化,这意味着我不能使用基于试错的固定向量。