谁能指导我如何使用matplotlib在极坐标图旁边放置垂直轴?

http://www.originlab.com/doc/Origin-Help/Polar-Graph引用一个示例,以说明所需的结果。

如图所示,左边是我的极坐标图中想要的垂直条,我想在matplotlib中复制它:
python - 极坐标图旁边的垂直 Axis-LMLPHP

编辑:这是我想向其中添加垂直轴的代码示例。

import matplotlib.pyplot as plt
import numpy as np

def sin_func(array):
    final = np.array([])
    for value in array:
        final = np.append(final, abs(np.sin(value)))
    return final

x = np.arange(0, 4*np.pi, 0.1)
y = sin_func(x)

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')

plt.plot(x, y)
# Changing axis to pi scale
ax.set_ylim([0, 1.2])
x_tick = np.arange(0, 2, 0.25)
x_label = [r"$" + format(r, '.2g') + r"\pi$" for r in x_tick]
ax.set_xticks(x_tick*np.pi)
ax.set_xticklabels(x_label, fontsize=10)
ax.set_rlabel_position(110)

plt.show()


python - 极坐标图旁边的垂直 Axis-LMLPHP

最佳答案

使用add_axes方法在所需位置添加其他轴,然后根据需要设置刻度位置和标签:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import AutoMinorLocator

def sin_func(array):
    final = np.array([])
    for value in array:
        final = np.append(final, abs(np.sin(value)))
    return final

x = np.arange(0, 4*np.pi, 0.1)
y = sin_func(x)

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')

plt.plot(x, y)

# Changing axis to pi scale
ax.set_ylim([0, 1.2])
x_tick = np.arange(0, 2, 0.25)
x_label = [r"$" + format(r, '.2g') + r"\pi$" for r in x_tick]
ax.set_xticks(x_tick*np.pi)
ax.set_xticklabels(x_label, fontsize=10)
ax.set_rlabel_position(110)

# Add Cartesian axes
ax2 = fig.add_axes((.1,.1,.0,.8))
ax2.xaxis.set_visible(False) # hide x axis
ax2.set_yticks(np.linspace(0,1,7)) # set new tick positions
ax2.set_yticklabels(['60 %','40 %', '20 %', '0 %', '20 %', '40 %', '60 %'])
ax2.yaxis.set_minor_locator(AutoMinorLocator(2)) # set minor tick for every second tick

plt.show()


python - 极坐标图旁边的垂直 Axis-LMLPHP

关于python - 极坐标图旁边的垂直 Axis ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45457786/

10-14 03:59