【发布时间】:2018-01-16 20:15:46
【问题描述】:
我开始尝试在 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、set_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 的 answer 中所建议的那样。
【问题讨论】:
-
@ImportanceOfBeingErnest 我认为这是“角度”一词在前两个轴中的位置
-
哦,可能是右上角Axes中“Magnitude”这个词的位置
-
抱歉,我应该更清楚一点:我想不出一种方法来去除半圆极坐标图周围的空白,特别是。为了清楚起见,我将编辑问题;感谢您的反馈。
标签: python matplotlib plot