我想不出如何使图例不与我的图重叠(见下图)。问题是我的坐标轴很复杂,因为它们来自windrose。要获取轴,请执行以下操作:
1)我已经从https://github.com/akrherz/windrose/tree/darylchanges下载了windrose.py
2)我使用python脚本example.py将windrose.py复制到相同的路径中
3)根据Subplot of Windrose in matplotlib中的步骤,我更改了windrose.py,使其能够进行子图。这些步骤是将WindroseAxes作为到matplotlib的投影。我编辑了文件windrose.py:
3a)包括一个
import from matplotlib.projections import register_projection 在文件的开头。
3b)然后添加一个name变量:
class WindroseAxes(PolarAxes):
name = 'windrose'
...3c)最后,在windrose.py的末尾添加:
register_projection(WindroseAxes)完成后,您可以使用matplotlib轴的projection参数轻松创建windrose轴。
4)现在我运行我的脚本如下(我的真实脚本示例)
from windrose import WindroseAxes
import numpy as np
import matplotlib.pyplot as plt
from windrose_subplot import WindroseAxes
wind_speeds1 = np.array([12,10,13,15])
wind_dirs1 = np.array([60,76,32,80]) # in degrees
wind_speeds2 = np.array([23,12,10,8])
wind_dirs2 = np.array([23,45,29,13])
fig = plt.figure()
ax1 = fig.add_subplot(231,projection='windrose')
ax1.bar(wind_dirs1,wind_speeds1,normed=True,opening=0.8,edgecolor='white')
ax2 = fig.add_subplot(232,projection='windrose')
ax2.bar(wind_dirs2,wind_speeds2,normed=True,opening=0.8,edgecolor='white')
ax1.legend()
ax2.legend()
plt.tight_layout()
plt.show()理想情况下,我希望用所有子图的最大/最小创建一个图例,因为它们都是相同的单位。此图例必须是每个子图的对应颜色,每个子图的值都相同(例如,与所有子图相关的单个普通图例)。在真实的脚本中将有6个子情节,但这里的2个现在显示了这一点。

发布于 2018-03-24 03:44:12
这很容易修复。为了只绘制一个图例,请注释掉或删除绘制第一个图例的位置。要将图例移出绘图,请使用带有某个逻辑位置的bbox_to_anchor=()。请参见下面的示例,该示例适用于此示例。
import numpy as np
import matplotlib.pyplot as plt
from windrose_subplot import WindroseAxes
wind_speeds1 = np.array([12,10,13,15])
wind_dirs1 = np.array([60,76,32,80]) # in degrees
wind_speeds2 = np.array([23,12,10,8])
wind_dirs2 = np.array([23,45,29,13])
fig = plt.figure()
ax1 = fig.add_subplot(231,projection='windrose')
ax1.bar(wind_dirs1,wind_speeds1,normed=True,opening=0.8,edgecolor='white')
ax2 = fig.add_subplot(232,projection='windrose')
ax2.bar(wind_dirs2,wind_speeds2,normed=True,opening=0.8,edgecolor='white')
# ax1.legend()
ax2.legend(bbox_to_anchor=(1.2 , -0.1))
plt.tight_layout()
plt.show()

但是,请注意,bbox_to_anchor依赖于图例所在的轴,因此
ax1.legend(bbox_to_anchor=1.2, -0.1))
#ax2.legend()将在第二个轴下面显示图例:

发布于 2022-03-01 13:43:52
谢谢你,Hazard11,我发现你的答案非常有用:)答案有一个问题,虽然图例并不代表第一个子图,因为箱是在创建第二个子图时生成的。
我刚刚解决了这个问题,首先使用numpy.histogram计算箱子,然后在创建每个风玫瑰时将其传递给windrose.WindroseAxes.bar()。这样做意味着您需要选择要用来生成回收站的回收站。另一种方法是手动定义bin,或者创建一个函数,为这两个库生成一些有效的bin,然后可以使用。
wind_speeds1 = np.array([12,10,13,15])
wind_dirs1 = np.array([60,76,32,80]) # in degrees
wind_speeds2 = np.array([23,12,10,8])
wind_dirs2 = np.array([23,45,29,13])
wind_speeds_bins = np.histogram(wind_speeds2, 5)[1]
fig = plt.figure()
ax1 = fig.add_subplot(231, projection='windrose')
ax1.bar(wind_dirs1 ,wind_speeds1, normed=True, opening=0.8, edgecolor='white', bins=wind_speeds_bins)
ax2 = fig.add_subplot(232, projection='windrose')
ax2.bar(wind_dirs2, wind_speeds2, normed=True, opening=0.8, edgecolor='white', bins=wind_speeds_bins)
# ax1.legend()
ax2.legend(bbox_to_anchor=(1.2 , -0.1))
plt.tight_layout()
plt.show()https://stackoverflow.com/questions/49420963
复制相似问题