在使用matplotlib制作一些半极性散点图时,我发现这些数字包含的空白区域远远大于笛卡尔图上绘制的相同数据。
问题似乎是PolarAxes类假设数据比率为1,即使是半极坐标图,我也找不到改变它的方法。有一个get_data_ratio操作,但没有set_data_ratio。我用A#标记了轴的4个角,用F#标记了图的四个角以突出显示问题。在极地情况下,轴角远离图表范围的角落,因此图表的垂直范围限制了其整体尺寸。
有人可以给我指导如何调整轴以更接近图表的范围吗?我用来生成两个图的代码,基于matplotlib极坐标示例如下。
import argparse
import numpy as np
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser(description='Plot max particle temperature at initial particle location')
parser.add_argument('--polar', action='store_true', help='use polar grid')
args = parser.parse_args()
polar = args.polar
# Fixing random state for reproducibility
np.random.seed(19680801)
# Compute radius and theta
N =100
r = 2 * np.random.rand(N)
theta = np.pi * np.random.rand(N)
area = 100 * r**2
colors = theta
# Compute Cartesian
x = r*np.cos(theta)
y = r*np.sin(theta)
# Choose polar or cartesian plot
if polar:
coor1=theta
coor2=r
else:
coor1=x
coor2=y
# Create rectangular figure
fig=plt.figure(figsize=(10,5))
# Create Axis
if polar:
ax = plt.axes(polar=True)
ax.set_thetamin(0)
ax.set_thetamax(180)
else:
ax = plt.axes()
# Plot data
c = ax.scatter(coor1, coor2, c=colors, cmap='hsv')
# Set limits
if polar:
ax.set_rlim(0,2)
else:
plt.axis('scaled')
plt.xlim(-2,2)
plt.ylim(0,2)
ax.annotate('A1',xy=(0, 0),xycoords='axes fraction',horizontalalignment='center', verticalalignment='center')
ax.annotate('A2',xy=(1, 0),xycoords='axes fraction',horizontalalignment='center', verticalalignment='center')
ax.annotate('A3',xy=(0, 1),xycoords='axes fraction',horizontalalignment='center', verticalalignment='center')
ax.annotate('A4',xy=(1, 1),xycoords='axes fraction',horizontalalignment='center', verticalalignment='center')
ax.annotate('F1',xy=(0, 0),xycoords='figure fraction',horizontalalignment='center', verticalalignment='center')
ax.annotate('F2',xy=(1, 0),xycoords='figure fraction',horizontalalignment='center', verticalalignment='center')
ax.annotate('F3',xy=(0, 1),xycoords='figure fraction',horizontalalignment='center', verticalalignment='center')
ax.annotate('F4',xy=(1, 1),xycoords='figure fraction',horizontalalignment='center', verticalalignment='center')
plt.show()