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

fab*_*ono 5 python plot matplotlib

我开始尝试在 Matplotlib 中创建不包含整个圆的极坐标图 - 即“楔形”图 - 通过设置thetaminthetamax属性。这是我期待已久的事情,我很高兴他们做到了 :)

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

下面是一个例子:

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')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

标签位于尴尬的位置并位于刻度标签上方,但可以通过向,命令添加额外labelpad参数将其移近或远离轴,因此这不是大问题。set_xlabelset_ylabel

不幸的是,我的印象是图被调整以适应现有的轴尺寸,这反过来导致半圆图上方和下方非常尴尬的空白(这当然是我需要使用的)。

这听起来应该很容易摆脱 - 我的意思是,楔形图是自动执行的 - 但我似乎无法弄清楚如何为半圆做它。任何人都可以对此有所了解吗?


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

这是通常tight_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')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


编辑 2:ImportanteOfBeingErnest回答中所建议的,为数字添加了背景颜色以更好地显示边界。

Imp*_*est 4

似乎“截断”极轴的楔形被放置在原始轴的中间。游戏中似乎有一些叫做LockedBBox和 的结构_WedgeBbox,我以前从未见过,也不完全理解。这些似乎是在绘制时创建的,因此从外部操纵它们似乎介于困难和不可能之间。

一种技巧是操纵原始轴,使生成的楔形出现在所需的位置。这并不是真正的确定性,而是通过反复试验寻找一些好的值。

在这种情况下要调整的参数是图形大小(figsize)、标签的填充(labelpad,如问题中已经指出的)以及最后的轴位置(ax.set_position([left, bottom, width, height]))。

结果可能看起来像

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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