在 Matplotlib 中删除楔形极坐标图周围的空间 [英] Removing space around wedge polar plots in Matplotlib

查看:55
本文介绍了在 Matplotlib 中删除楔形极坐标图周围的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始尝试通过设置 thetamin thetamax 在Matplotlib中创建不包含整个圆的极坐标图(即楔形"图)特性.这是我等待了很长时间的事情,我很高兴他们完成了:)

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

这是一个例子:

将 numpy 导入为 np导入matplotlib.pyplot作为plt#连续获取4个极轴图,轴= plt.subplots(2,2,subplot_kw = {'projection':'polar'},figsize =(8,8))#设置facecolor以更好地显示边界#(根据ImportanceOfBeingErnest 的建议)fig.set_facecolor('paleturquoise')对于 i,枚举中的 theta_max([2*np.pi, np.pi, 2*np.pi/3, np.pi/3]):#定义端点变化的theta向量和一些要绘制的数据theta = np.linspace(0,theta_max,181)数据=(1/6)* np.abs(np.sin(3 * theta)/np.sin(theta/2))# 根据数据设置'thetamin'和'thetamax'轴[i//2, i%2].set_thetamin(0)轴[i//2, i%2].set_thetamax(theta_max*180/np.pi)#实际绘制数据,微调半径限制并添加标签axes [i//2,i%2] .plot(θ,数据)轴[i//2, i%2].set_ylim([0, 1])axis[i//2, i%2].set_xlabel('Magnitude', fontsize=15)axis[i//2, i%2].set_ylabel('Angles', fontsize=15)fig.set_tight_layout(真)#fig.savefig('fig.png',facecolor ='skyblue')

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

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

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

<小时>

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

这通常是 tight_layout()负责的事情,但在这里似乎并没有解决问题.我尝试在绘制后手动更改图形窗口的大小,但是空白只是随更改而缩放.下面是一个最小的工作示例;如果我愿意,我可以让 xlabel 更接近图像,但保存的图像文件仍然包含大量空白.

有谁知道怎么去掉这个空格?

将 numpy 导入为 np导入matplotlib.pyplot作为plt# 得到一个半圆极坐标图fig1,ax1 = plt.subplots(1,1,subplot_kw = {'projection':'polar'})# 设置 facecolor 以更好地显示边界#(由ImportanceOfBeingErnest建议)fig1.set_facecolor('天蓝色')theta_min = 0theta_max = np.pitheta = np.linspace(theta_min, theta_max, 181)数据=(1/6)* np.abs(np.sin(3 * theta)/np.sin(theta/2))# 根据数据设置'thetamin'和'thetamax'ax1.set_thetamin(0)ax1.set_thetamax(theta_max * 180/np.pi)#实际绘制数据,微调半径限制并添加标签ax1.plot(θ,数据)ax1.set_ylim([0, 1])ax1.set_xlabel('Magnitude', fontsize=15)ax1.set_ylabel('Angles',fontsize = 15)fig1.set_tight_layout(真)#fig1.savefig('fig1.png',facecolor ='skyblue')

<小时>

编辑 2: 为图形添加背景颜色以更好地显示边界,如 ImportanteOfBeingErnest

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

I am starting to play around with creating polar plots in Matplotlib that do NOT encompass an entire circle - i.e. a "wedge" plot - by setting the thetamin and thetamax properties. This is something I was waiting for for a long time, and I am glad they have it done :)

However, I have noticed that the figure location inside the axes seem to change in a strange manner when using this feature; depending on the wedge angular aperture, it can be difficult to fine tune the figure so it looks nice.

Here's an example:

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

The labels are in awkward locations and over the tick labels, but can be moved closer or further away from the axes by adding an extra labelpad parameter to set_xlabel, set_ylabel commands, so it's not a big issue.

Unfortunately, I have the impression that the plot is adjusted to fit inside the existing axes dimensions, which in turn lead to a very awkward white space above and below the half circle plot (which of course is the one I need to use).

It sounds like something that should be reasonably easy to get rid of - I mean, the wedge plots are doing it automatically - but I can't seem to figure it out how to do it for the half circle. Can anyone shed a light on this?


EDIT: Apologies, my question was not very clear; I want to create a half circle polar plot, but it seems that using set_thetamin() you end up with large amounts of white space around the image (especially above and below) which I would rather have removed, if possible.

It's the kind of stuff that normally tight_layout() takes care of, but it doesn't seem to be doing the trick here. I tried manually changing the figure window size after plotting, but the white space simply scales with the changes. Below is a minimum working example; I can get the xlabel closer to the image if I want to, but saved image file still contains tons of white space around it.

Does anyone knows how to remove this white space?

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


EDIT 2: Added background color to figures to better show the boundaries, as suggested in ImportanteOfBeingErnest's answer.

解决方案

It seems the wedge of the "truncated" polar axes is placed such that it sits in the middle of the original axes. There seems so be some constructs called LockedBBox and _WedgeBbox in the game, which I have never seen before and do not fully understand. Those seem to be created at draw time, such that manipulating them from the outside seems somewhere between hard and impossible.

One hack can be to manipulate the original axes such that the resulting wedge turns up at the desired position. This is not really deterministic, but rather looking for some good values by trial and error.

The parameters to adjust in this case are the figure size (figsize), the padding of the labels (labelpad, as already pointed out in the question) and finally the axes' position (ax.set_position([left, bottom, width, height])).

The result could then look like

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

Here I've set some color to the background of the figure to better see the boundary.

这篇关于在 Matplotlib 中删除楔形极坐标图周围的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆