【问题标题】:How to clip polar plot in pylab/pyplot如何在 pylab/pyplot 中剪辑极坐标图
【发布时间】:2014-12-11 22:06:53
【问题描述】:

我有一个极坐标图,其中theta 从 0 到 pi/2 变化,所以整个图位于第一季度,如下所示:

%pylab inline
X=linspace(0,pi/2)
polar(X,cos(6*X)**2)


(来源:schurov.com

是否可以通过 pyplot 或 matplotlib 剪辑极坐标图,以便只显示第一季度(因此不会浪费空间)?我想要这样的图片(但要适当缩放):


(来源:schurov.com

我想通过 pyplot 来实现,因为我有几张这样的图像,并且想将它们排列成一个大图(带有子图)。有可能吗?

【问题讨论】:

标签: python matplotlib plot polar-coordinates


【解决方案1】:

我建议不要使用 polar 绘图,而是设置轴艺术家。这允许您设置“部分”极坐标图。

此答案基于对第三个示例的调整:axes_grid example code: demo_floating_axes.py

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
import mpl_toolkits.axisartist.angle_helper as angle_helper
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist.grid_finder import MaxNLocator

# define how your plots look:

def setup_axes(fig, rect, theta, radius):

    # PolarAxes.PolarTransform takes radian. However, we want our coordinate
    # system in degree
    tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform()

    # Find grid values appropriate for the coordinate (degree).
    # The argument is an approximate number of grids.
    grid_locator1 = angle_helper.LocatorD(2)

    # And also use an appropriate formatter:
    tick_formatter1 = angle_helper.FormatterDMS()

    # set up number of ticks for the r-axis
    grid_locator2 = MaxNLocator(4)

    # the extremes are passed to the function
    grid_helper = floating_axes.GridHelperCurveLinear(tr,
                                extremes=(theta[0], theta[1], radius[0], radius[1]),
                                grid_locator1=grid_locator1,
                                grid_locator2=grid_locator2,
                                tick_formatter1=tick_formatter1,
                                tick_formatter2=None,
                                )

    ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
    fig.add_subplot(ax1)

    # adjust axis
    # the axis artist lets you call axis with
    # "bottom", "top", "left", "right"
    ax1.axis["left"].set_axis_direction("bottom")
    ax1.axis["right"].set_axis_direction("top")

    ax1.axis["bottom"].set_visible(False)
    ax1.axis["top"].set_axis_direction("bottom")
    ax1.axis["top"].toggle(ticklabels=True, label=True)
    ax1.axis["top"].major_ticklabels.set_axis_direction("top")
    ax1.axis["top"].label.set_axis_direction("top")

    ax1.axis["left"].label.set_text("R")
    ax1.axis["top"].label.set_text(ur"$\alpha$ [\u00b0]")

    # create a parasite axes
    aux_ax = ax1.get_aux_axes(tr)

    aux_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax
    ax1.patch.zorder=0.9 # but this has a side effect that the patch is
                         # drawn twice, and possibly over some other
                         # artists. So, we decrease the zorder a bit to
                         # prevent this.

    return ax1, aux_ax

#
# call the plot setup to generate 3 subplots
#

fig = plt.figure(1, figsize=(8, 4))
fig.subplots_adjust(wspace=0.3, left=0.05, right=0.95)

ax1, aux_ax1 = setup_axes(fig, 131, theta=[0, 90], radius=[0, 1])
ax2, aux_ax2 = setup_axes(fig, 132, theta=[0, 90], radius=[0, 1])
ax3, aux_ax3 = setup_axes(fig, 133, theta=[0, 90], radius=[0, 1])

#
# generate the data to plot
#
theta = np.linspace(0,90) # in degrees
radius = np.cos(6.*theta * pi/180.0)**2.0

# 
# populate the three subplots with the data
#
aux_ax1.plot(theta, radius, 'r')
aux_ax2.plot(theta, radius, 'b')
aux_ax3.plot(theta, radius, 'g')

plt.show()  

你最终会得到这个情节:

axisartist referencedemo on a curvelinear grid 提供了一些关于如何使用angle_helper 的额外见解。特别是关于哪些部分需要弧度以及哪些度数。

【讨论】:

    猜你喜欢
    • 2013-06-13
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    相关资源
    最近更新 更多