【问题标题】:Removing space around wedge polar plots in Matplotlib在 Matplotlib 中删除楔形极坐标图周围的空间
【发布时间】:2018-01-16 20:15:46
【问题描述】:

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

标签位于不合适的位置和刻度标签上方,但可以通过在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: 为图形添加背景颜色以更好地显示边界,如 ImportanteOfBeingErnestanswer 中所建议的那样。

【问题讨论】:

  • @ImportanceOfBeingErnest 我认为这是“角度”一词在前两个轴中的位置
  • 哦,可能是右上角Axes中“Magnitude”这个词的位置
  • 抱歉,我应该更清楚一点:我想不出一种方法来去除半圆极坐标图周围的空白,特别是。为了清楚起见,我将编辑问题;感谢您的反馈。

标签: python matplotlib plot


【解决方案1】:

“截断”极轴的楔形似乎放置在原始轴的中间。游戏中似乎有一些称为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()

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

【讨论】:

  • 太好了,这正是我要找的 :) 非常感谢!如此有效地,您的想法是通过设置轴的位置 outside 来补偿空白,是这样吗?无论如何,它确实成功了。我可以通过反复试验微调“模板”,然后用它来绘制类似的数字,所以这对我来说不是问题。我也喜欢设置背景颜色以显示相邻空间的想法;也会编辑问题中的数字来做到这一点!
  • 我打开了一个关于此的问题:github.com/matplotlib/matplotlib/issues/10264 不过,这更像是一个愿望清单问题,我不希望在不久的将来会改变。
  • 谢谢,非常感谢;总的来说,我对这种用于极地情节的新能力感到非常满意,所以这个白色空间的东西在路上是一个相当小的颠簸。如果它在中距离的未来得到分类,那就太好了;如果没有,那也没关系:)
  • 是否有可能获得所需的外部坐标以迭代方式估计set_position() 参数?我有一个楔形极坐标图与set_rorigin(0) 和动态轴限制相结合,这会导致绘图尺寸发生变化,这意味着我不能使用基于反复试验的固定向量。