未解决的问题:要同时查看背景图像和极坐标图,我需要使图像透明(ax_image.imshow(img, alpha = .5)).我敢打赌,有一种方法可以使用各种剧情元素的zorder来避免这种情况,但是我无法实现.此外,图像超出极坐标图.可以通过添加形状正确的补丁和zorder来阻止图像的不需要的部分来解决此问题,但是我不确定问问者对此有何疑问.它看起来应该像import numpy as npimport matplotlib.pyplot as pltimport csvr = list(csv.reader(open('avg.csv', 'r')))theta = np.linspace(0, 2 * np.pi, 73)img = plt.imread("voltage_abs.png")fig = plt.gcf()axes_coords = [0.1, 0.1, 0.8, 0.8]ax_polar = fig.add_axes(axes_coords, projection = 'polar')ax_polar.patch.set_alpha(0)ax_polar.plot(theta, r)ax_polar.set_ylim(30, 41)ax_polar.set_yticks(np.arange(30, 41, 2))ax_polar.set_yticklabels([])ax_polar.set_rlabel_position(-22.5) # get radial labels away from plotted lineax_polar.grid(True)ax_polar.set_title("Polar", va = 'bottom')ax_image = fig.add_axes(axes_coords)ax_image.imshow(img, alpha = .5)ax_image.axis('off') # don't show the axes ticks/lines/etc. associated with the imageplt.savefig('polar.pdf', bbox_inches = 'tight')plt.show()设置轴刻度/标签/标题/等.可能需要使所有内容看起来都很好.顺便说一句,万一有人落在这里寻找如何对矩形轴进行操作,则无需做两轴技巧:只需在要绘制的相同轴上按imshow并设置alpha/zorder合适.I am trying to plot a polar plot on top of an image. Here is the code I have, I just can't get the polar plot to be transparent so that the picture is seen underneath:import numpy as npimport matplotlib.pyplot as pltimport csvr = list(csv.reader(open('avg.csv','r')))theta = np.linspace(0, 2 * np.pi, 73)img = plt.imread("voltage_abs.png")imgplot = plt.imshow(img)fig, ax = plt.subplots()ax.imshow(img, zorder=0)ax.imshow(img)ax = plt.subplot(111, projection='polar')ax.plot(theta, r)ax.set_ylim(30,41)ax.set_yticks(np.arange(30,41,2))ax.set_yticklabels([])ax.set_rlabel_position(-22.5) # get radial labels away from plotted lineax.grid(True)ax.set_title("Polar", va='bottom')plt.savefig('polar.pdf',bbox_inches='tight')plt.show()Any ideas? 解决方案 Partial solution:Create two axes manually, on top of each other, one to display the image, and the other to display the polar plot. The trick is to make the background patch of the polar axis transparent via ax_polar.path.set_alpha(0). I believe the problem with the approach in the question is that imshow is not behaving nicely when used on a polar-projection axis, so no amount of playing with alpha/zorder will make it work as desired.Unsolved issues: To see both the background image and the polar plot, I needed to make the image transparent (ax_image.imshow(img, alpha = .5)). I bet there's a way to play with the zorder of various plot elements to avoid this, but I wasn't able to make it happen.Also, the image extends beyond the polar plot. This could probably be fixed by adding a patch with the right shape and zorder to block the undesired parts of the image, but I'm not sure what the question asker wants with regards to that.It should look something likeimport numpy as npimport matplotlib.pyplot as pltimport csvr = list(csv.reader(open('avg.csv', 'r')))theta = np.linspace(0, 2 * np.pi, 73)img = plt.imread("voltage_abs.png")fig = plt.gcf()axes_coords = [0.1, 0.1, 0.8, 0.8]ax_polar = fig.add_axes(axes_coords, projection = 'polar')ax_polar.patch.set_alpha(0)ax_polar.plot(theta, r)ax_polar.set_ylim(30, 41)ax_polar.set_yticks(np.arange(30, 41, 2))ax_polar.set_yticklabels([])ax_polar.set_rlabel_position(-22.5) # get radial labels away from plotted lineax_polar.grid(True)ax_polar.set_title("Polar", va = 'bottom')ax_image = fig.add_axes(axes_coords)ax_image.imshow(img, alpha = .5)ax_image.axis('off') # don't show the axes ticks/lines/etc. associated with the imageplt.savefig('polar.pdf', bbox_inches = 'tight')plt.show()Fiddling with the axis ticks/labels/title/etc. will likely be necessary to make everything look nice.Incidentally, in case anyone lands here looking for how to do this for rectangular axes, you don't need to do the two-axes trick: just imshow on the same axes you're plotting on and set the alpha/zorder appropriately. 这篇关于在图像上方绘制极坐标图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 05:25