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

5
我开始尝试在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')

enter image description here

标签位置不太合适,有些覆盖了刻度标签,但是可以通过在set_xlabelset_ylabel命令中添加额外的labelpad参数来使其靠近或远离轴线,所以这不是一个大问题。

不幸的是,我认为图表被调整以适应现有轴线尺寸,这反过来导致半圆形图表上方和下方出现非常尴尬的白色空间(当然,这正是我需要使用的部分)。

这听起来应该很容易解决 - 我的意思是楔形图自动完成了这个功能 - 但是我似乎无法找到如何对半圆形图表进行操作。是否有人能够解决这个问题?


编辑:抱歉,我的问题没有表述清楚;我想创建一个半圆形极坐标图,但是使用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')

这里输入图片描述


编辑2:根据ImportanteOfBeingErnest回答建议,为图形添加背景颜色以更好地显示边界。


@ImportanceOfBeingErnest 我认为“Angles”的位置是在前两个轴中。 - Paul H
哦,也许是“Magnitude”一词在右上角坐标轴中的位置。 - Paul H
抱歉,我应该表述得更清楚:我无法找到一种方法来移除半圆极坐标图周围的空白区域。我将编辑问题以便更加清晰;感谢您的反馈。 - fabio_hirono
1个回答

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()

enter image description here

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


太棒了,这正是我在寻找的 :) 非常感谢!所以,您的想法实际上是通过将轴的位置设置在图形边界之外来补偿空格,是这样吗?无论如何,这肯定起到了作用;我可以通过试错来微调“模板”,然后使用它来绘制类似的图形,所以这对我来说不是问题。我也喜欢将背景颜色设置为显示相邻空间的想法;将编辑问题中的图形以执行此操作! - fabio_hirono
1
我在这个问题上开了一个issue:https://github.com/matplotlib/matplotlib/issues/10264。虽然这更像是一个愿望清单的问题,但我不指望它会在不久的将来改变。 - ImportanceOfBeingErnest
谢谢,感激不尽;总的来说,我对极坐标图的这种新能力非常满意,所以这个白色空间问题只是一条小小的路障。如果在不久的将来得到解决,那太好了;如果没有,也没关系 :) - fabio_hirono
是否有可能通过迭代的方式获取所需的外部坐标来估计 set_position() 参数?我使用了楔形极坐标图与 set_rorigin(0) 和动态轴限制相结合,导致绘图尺寸发生变化,这意味着我不能使用基于试错的固定向量。 - brnk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接