简体   繁体   中英

Plot polar plot on top of image?

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 np
import matplotlib.pyplot as plt
import csv


r = 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 line
ax.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 like

import numpy as np
import matplotlib.pyplot as plt
import csv

r = 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 line
ax_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 image

plt.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM