在创建曲线坐标轴之后,我正在尝试重新定位和拉伸用floating_axes.FloatingSubplot创建的曲线坐标轴。在matplotlib的旧版本中,我可以使用以下代码成功完成此操作:
horiz = [Size.Scaled(1.0)]
vert = [Size.Scaled(1.0)]
ax1_div = Divider(fig,[0.0, 0.0, 1.0, 0.45],horiz,vert,aspect=False) # Curvilinear Coordinates
ax1_loc = ax1_div.new_locator(nx=0,ny=0) # Curvilinear Coordinates
ax1.set_axes_locator(ax1_loc) # Curvilinear Coordinates在下图中,我希望能够拉伸曲线坐标系,使其x轴与矩形坐标系对齐。旧版本的matplotlib可以做到这一点,但在matplotlib版本3.3.4中,我只能让轴进行平移,而不能进行拉伸。
有人知道怎么做吗?ax.set_position()不能在这里工作。我的问题和this question which received no answers类似。
下面是我生成该图的代码:
#!/usr/bin/env python3
from matplotlib import pyplot as plt
from matplotlib.transforms import Affine2D, Transform
import mpl_toolkits.axisartist.floating_axes as floating_axes
from matplotlib.projections import polar
from mpl_toolkits.axisartist.grid_finder import FixedLocator, DictFormatter
import mpl_toolkits.axes_grid1.axes_size as Size
from mpl_toolkits.axes_grid1 import Divider
fig = plt.figure()
# Generate first axis using normal methods.
ax0 = fig.add_subplot(111)
ax0.set_xticks([0,500,1000,1500,2000])
# Generate a curvilear axis for the second axis.
angle_ticks = [(0, '0'), (0.079, '500'), (0.157, '1000'), (0.235, '1500'), (0.314, '2000')]
grid_locator1 = FixedLocator([v for v, s in angle_ticks])
tick_formatter1 = DictFormatter(dict(angle_ticks))
alt_ticks = [(6371.0, '0'), (6671.0, '300'), (6971.0, '600'), (7271.0, '900'), (7571.0, '1200')]
grid_locator2 = FixedLocator([v for v, s in alt_ticks])
tick_formatter2 = DictFormatter(dict(alt_ticks))
tr_rotate = Affine2D().rotate(1.414)
tr_shift = Affine2D().translate(0, 6371)
tr = polar.PolarTransform() + tr_rotate
grid_helper = \
floating_axes.GridHelperCurveLinear(tr, extremes=(0, 0.314, 6371, 7871),
grid_locator1=grid_locator1,
grid_locator2=grid_locator2,
tick_formatter1=tick_formatter1,
tick_formatter2=tick_formatter2,)
ax1 = floating_axes.FloatingSubplot(fig, 111, grid_helper=grid_helper)
ax1.invert_xaxis()
fig.add_subplot(ax1, transform=tr)
horiz = [Size.Scaled(1.0)]
vert = [Size.Scaled(1.0)]
ax0_div = Divider(fig,[0.0, 0.5, 1.0, 0.45],horiz,vert,aspect=False) # Rectangular Coordinates
ax1_div = Divider(fig,[0.0, 0.0, 1.0, 0.45],horiz,vert,aspect=False) # Curvilinear Coordinates
ax0_loc = ax0_div.new_locator(nx=0,ny=0) # Rectangular Coordinates
ax1_loc = ax1_div.new_locator(nx=0,ny=0) # Curvilinear Coordinates
ax0.set_axes_locator(ax0_loc) # Rectangular Coordinates
ax1.set_axes_locator(ax1_loc) # Curvilinear Coordinates
fig.savefig('bug_report.png',bbox_inches='tight')发布于 2021-03-31 13:19:02
我想知道你是否真的需要一个轴分隔器。仅仅使用子图的网格不就足够了吗?请看一下下面的代码片段和它生成的图形。
#!/usr/bin/env python3
from matplotlib.projections.polar import PolarTransform
from matplotlib.pyplot import figure
from matplotlib.transforms import Affine2D
from mpl_toolkits.axisartist.floating_axes import (
FloatingAxes, GridHelperCurveLinear)
from mpl_toolkits.axisartist.grid_finder import FixedLocator, DictFormatter
def get_locator_formatter(ticks):
return FixedLocator([v for v, s in ticks]), DictFormatter(dict(ticks))
fig = figure(figsize=(8, 8), constrained_layout=True)
ax0 = fig.add_subplot(211)
ax0.set_xticks([0, 500, 1000, 1500, 2000])
ax0.set_yticks([0, 300, 600, 900, 1200])
ax0.set_aspect('equal', adjustable='box')
angle_ticks = [(0, '0'), (0.079, '500'), (0.157, '1000'),
(0.235, '1500'), (0.314, '2000')]
alt_ticks = [(6371.0, '0'), (6671.0, '300'), (6971.0, '600'),
(7271.0, '900'), (7571.0, '1200')]
grid_locator1, tick_formatter1 = get_locator_formatter(angle_ticks)
grid_locator2, tick_formatter2 = get_locator_formatter(alt_ticks)
tr = PolarTransform() + Affine2D().rotate(1.414)
grid_helper = GridHelperCurveLinear(
tr, extremes=(0, 0.314, 6371, 7871),
grid_locator1=grid_locator1, grid_locator2=grid_locator2,
tick_formatter1=tick_formatter1, tick_formatter2=tick_formatter2,)
ax1 = fig.add_subplot(212, axes_class=FloatingAxes, grid_helper=grid_helper)
ax1.invert_xaxis()
fig.savefig('bug_report.png', bbox_inches='tight')

https://stackoverflow.com/questions/66881013
复制相似问题