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

标签 python matplotlib plot axis

有人可以指导我如何使用 matplotlib 在极坐标图旁边放置垂直 Axis 吗?

引用http://www.originlab.com/doc/Origin-Help/Polar-Graph中的例子,期望的结果。

如图所示,左侧是极坐标图中所需的垂直条,我想在 matplotlib 中重现它: enter image description here

编辑:这是我想要添加垂直 Axis 的代码示例。

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

enter image description here

最佳答案

使用 add_axes 添加附加 Axis 方法放置您想要的位置,然后根据需要设置刻度位置和标签:

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

enter image description here

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

相关文章:

python - 未使用 os.path.isfile 函数识别现有文件

java - Java 中的默认值类似于 python

python - Scrapy 不会生成 DNS 查找失败网站的网站 URL

python-3.x - 具有嵌套行的 Seaborn 热图

python - 将正态分布拟合到一维数据

r - R 条形图的组着色错误

python - 在 for 循环中绘图,在 matplotlib 中具有 'hold on' 效果?

Python Plot,仅从图例中删除标签

python - 异常值 : 'DatabaseWrapper' object has no attribute 'Database'

python - 无法隐藏子图轴标签或在 matplotlib 中设置 MaxNLocator