在图像上方绘制极坐标图? [英] Plot polar plot on top of image?

查看:102
本文介绍了在图像上方绘制极坐标图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在图像上方绘制极坐标图.这是我的代码,我只是无法使极坐标图变得透明,因此无法在下面看到图片:

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()

有什么想法吗?

解决方案

部分解决方案:

手动创建两个坐标轴,两个坐标轴彼此重叠,一个坐标轴显示图像,另一个坐标轴显示极坐标图.诀窍是通过ax_polar.path.set_alpha(0)使极轴的背景色块透明.我相信问题中方法的问题在于imshow在极投影轴上使用时表现不佳,因此没有必要大量使用alpha/zorder进行操作.

未解决的问题:要同时查看背景图像和极坐标图,我需要使图像透明(ax_image.imshow(img, alpha = .5)).我敢打赌,有一种方法可以使用各种剧情元素的zorder来避免这种情况,但是我无法实现.

此外,图像超出极坐标图.可以通过添加形状正确的补丁和zorder来阻止图像的不需要的部分来解决此问题,但是我不确定问问者对此有何疑问.

它看起来应该像

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()

设置轴刻度/标签/标题/等.可能需要使所有内容看起来都很好.

顺便说一句,万一有人落在这里寻找如何对矩形轴进行操作,则无需做两轴技巧:只需在要绘制的相同轴上按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 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.

这篇关于在图像上方绘制极坐标图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆