如何在matplotlib中的极坐标图中添加笛卡尔轴?

时间:2019-07-05 10:15:05

标签: python matplotlib polar-coordinates

我有一个极坐标轮廓图,我想向其添加笛卡尔坐标轴。我怎样才能做到这一点?似乎没有任何文档。

import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
    return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)

x = np.linspace(0, 8, 50)
y = np.linspace(0, 8, 40)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig, ax = plt.subplots(subplot_kw = dict(projection = 'polar'))
plt.axis('off')
ax.contourf(X, Y, Z)

1 个答案:

答案 0 :(得分:-1)

要将笛卡尔坐标轴添加到极坐标图中,您要做的就是在同一位置添加第二对坐标轴,如下所示:

import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
    return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)

x = np.linspace(0, 8, 50)
y = np.linspace(0, 8, 40)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig, ax = plt.subplots(subplot_kw = dict(projection = 'polar'))
plt.axis('off')
ax.contourf(X, Y, Z)

new_axis = fig.add_axes(ax.get_position(), frameon = False)
new_axis.plot()
plt.show()