此处的文档中有一个很好的曲线轴网格示例。这假设您有一个空图,然后使用以下命令创建曲线轴:
ax1 = Subplot(fig, 1, 2, 1, grid_helper=grid_helper)
但是,我正在编写一个给定现有轴的函数,并且我希望该函数ax用新的曲线轴替换该轴 ( )。
到目前为止,这是我的功能的开始:
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist import SubplotHost
from mpl_toolkits.axisartist.grid_helper_curvelinear import GridHelperCurveLinear
import mpl_toolkits.axisartist.angle_helper as angle_helper
from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D
def sgrid(ax=None):
"""
Adds an s-plane grid of constant damping factors and natural
frequencies to a plot. If ax is not specified, the current
figure axis is used.
Parameters
----------
ax : matplotlib axis object
If not passed, uses gca() to get current axis.
Returns
-------
ax : matplotlib axis
"""
grid_helper = GridHelperCurveLinear(
... some stuff ...
)
if ax is None:
# Get the current axis or create a new figure with one
ax = plt.gca()
fig = ax.figure
# TODO: How can we change the axis if it is already created?
ax = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)
... (code that adds more stuff to ax) ...
return ax
另外,我不确定我是否理解SubplotHost. 这些是新轴的初始化参数还是什么?
更新
这里的目标是模拟 Pandas.Series.plot 函数的工作方式。所需的用例是这样的:
H = tf([2, 5, 1],[1, 2, 3])
rlocus(H)
sgrid()
plt.show()
或者
>>> fig, axes = plt.subplots(2, 1)
>>> rlocus(H1, ax=axes[1])
>>> rlocus(H2, ax=axes[2])
>>> for ax in axes:
>>> sgrid(ax=ax) # Later we might want to add ax.zgrid()
>>> plt.show()
sgrid理想情况下和的顺序rlocus应该如上,因为这类似于我们正在模拟的 MATLAB 函数以及plt.grid()它们相似的方法。