Matplotlib极坐标图径向轴偏移 [英] Matplotlib polar plot radial axis offset

查看:68
本文介绍了Matplotlib极坐标图径向轴偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否可以偏移径向轴的起点或将其移到图形之外.

这就是我希望实现的目标:

这就是我现在所拥有的.

我已阅读有关 SO 的文档和不同主题,但找不到任何有用的信息.这是否意味着如果没有在任何地方提及它甚至不可能.

谢谢.

编辑(添加用于创建绘图的代码片段):

ax = fig.add_subplot(111,projection='polar')ax.set_theta_zero_location('N')ax.set_theta_direction(-1)ax.plot(X,lines[li]*yScalingFactor,label=linelabels[li],color=color,linestyle=ls)

解决方案

要偏移径向轴的起点:

从 Matplotlib 2.2.3 开始,有一个名为 set_rorigin 的新 Axes 方法,它正是这样做的.您用原点的理论径向坐标来称呼它.因此,如果您调用 ax.set_ylim(0, 2)ax.set_rorigin(-1),则中心圆的半径将是圆心半径的三分之一情节.

Matplotlib<的快速而肮脏的解决方法2.2.3 是将下径向轴限制设置为负值并将绘图的内部部分隐藏在圆圈后面:

将 numpy 导入为 np导入matplotlib.pyplot作为pltCIRCLE_RES = 36 # 内圆的分辨率def offset_radial_axis(ax):x_circle = np.linspace(0,2 * np.pi,CIRCLE_RES)y_circle = np.zeros_like(x_circle)ax.fill(x_circle, y_circle, fc='white', ec='black', zorder=2) # circleax.set_rmin(-1)#需要在ax.fill之后.不知道为什么.ax.set_rticks([在 ax.get_yticks() 中勾选勾选,如果勾选 >= 0])# 或手动设置刻度(简单)# 或者定义一个自定义的 TickLocator(非常灵活)# 如果刻度完全在圆圈后面,则省略此行

要在图外添加比例:

您可以在其他轴的上半部添加一个额外的轴对象,并使用其y轴:

X_OFFSET = 0 # 控制比例尺离图多远(轴坐标)def add_scale(ax):#为刻度添加额外的轴rect = ax.get_position()rect =(rect.xmin-X_OFFSET,rect.ymin + rect.height/2,#x,yrect.width,rect.height/2)#宽度,高度scale_ax = ax.figure.add_axes(rect)#隐藏新轴的大多数元素对于 ['right', 'top', 'bottom'] 中的 loc:scale_ax.spines[loc].set_visible(False)scale_ax.tick_params(bottom = False,labelbottom = False)scale_ax.patch.set_visible(False) # 隐藏白色背景# 调整比例scale_ax.spines ['left'].set_bounds(* ax.get_ylim())#scale_ax.spines ['left'].set_bounds(0,ax.get_rmax())#mpl<2.2.3scale_ax.set_yticks(ax.get_yticks())scale_ax.set_ylim(ax.get_rorigin(), ax.get_rmax())# scale_ax.set_ylim(ax.get_ylim()) # Matplotlib <2.2.3

将它们放在一起:

(此示例摘自

I was wondering, is it possible to offset the start of the radial axis or move it outside of the graph.

This is what I'm hoping to achieve:

And this is what I have for now.

I have read the documentation and different topics on SO, but I couldn't find anything helpful. Does that mean that it is not even possible if it is not mentioned anywhere.

Thank you in advance.

EDIT (added snippet of a code used to create the plot):

ax = fig.add_subplot(111, projection='polar')
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)      
ax.plot(X,lines[li]*yScalingFactor,label=linelabels[li],color=color,linestyle=ls)

解决方案

To offset the start of the radial axis:

EDIT: As of Matplotlib 2.2.3 there's a new Axes method called set_rorigin which does exactly that. You call it with the theoretical radial coordinate of the origin. So if you call ax.set_ylim(0, 2) and ax.set_rorigin(-1), the radius of the center circle will be a third of the radius of the plot.

A quick and dirty workaround for Matplotlib < 2.2.3 is to set the lower radial axis limit to a negative value and hide the inner part of the plot behind a circle:

import numpy as np
import matplotlib.pyplot as plt

CIRCLE_RES = 36 # resolution of circle inside
def offset_radial_axis(ax):
    x_circle = np.linspace(0, 2*np.pi, CIRCLE_RES)
    y_circle = np.zeros_like(x_circle)
    ax.fill(x_circle, y_circle, fc='white', ec='black', zorder=2) # circle
    ax.set_rmin(-1) # needs to be after ax.fill. No idea why.
    ax.set_rticks([tick for tick in ax.get_yticks() if tick >= 0])
    # or set the ticks manually (simple)
    # or define a custom TickLocator (very flexible)
    # or leave out this line if the ticks are fully behind the circle

To add a scale outside the plot:

You can add an extra axes object in the upper half of the other axes and use its yaxis:

X_OFFSET = 0 # to control how far the scale is from the plot (axes coordinates)
def add_scale(ax):
    # add extra axes for the scale
    rect = ax.get_position()
    rect = (rect.xmin-X_OFFSET, rect.ymin+rect.height/2, # x, y
            rect.width, rect.height/2) # width, height
    scale_ax = ax.figure.add_axes(rect)
    # hide most elements of the new axes
    for loc in ['right', 'top', 'bottom']:
        scale_ax.spines[loc].set_visible(False)
    scale_ax.tick_params(bottom=False, labelbottom=False)
    scale_ax.patch.set_visible(False) # hide white background
    # adjust the scale
    scale_ax.spines['left'].set_bounds(*ax.get_ylim())
    # scale_ax.spines['left'].set_bounds(0, ax.get_rmax()) # mpl < 2.2.3
    scale_ax.set_yticks(ax.get_yticks())
    scale_ax.set_ylim(ax.get_rorigin(), ax.get_rmax())
    # scale_ax.set_ylim(ax.get_ylim()) # Matplotlib < 2.2.3

Putting it all together:

(The example is taken from the Matplotlib polar plot demo)

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r

ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
ax.grid(True)

ax.set_rorigin(-1)
# offset_radial_axis(ax) # Matplotlib < 2.2.3
add_scale(ax)

ax.set_title("A line plot on an offset polar axis", va='bottom')
plt.show()

这篇关于Matplotlib极坐标图径向轴偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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