2017-08-02 134 views
1

任何人都可以引導我如何使用matplotlib將垂直軸放在極座標圖旁邊?極座標圖旁邊的垂直軸

引用http://www.originlab.com/doc/Origin-Help/Polar-Graph的一個例子,得到期望的結果。

如該圖所示,在左邊是在我的極座標圖所希望的垂直條,我想在matplotlib重現: enter image description here

編輯:這是我希望的代碼的一個例子將垂直軸添加到。

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

+0

你嘗試過什麼?什麼不行?請閱讀[如何提問](https://stackoverflow.com/help/how-to-ask)。如果你表明你已經嘗試了一些東西,那麼你更有可能獲得幫助。 –

+1

該軸試圖表示什麼?我不明白爲什麼會在這裏想要。順便說一句,只要使用'y = np.abs(np.sin(x))'而不是自己的函數。 – Michael

+0

@Michael我想在那裏有軸,以便我可以刪除r標籤。謝謝你的建議。 – Tian

回答

3

添加額外的軸與位置你想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() 

enter image description here