2017-09-15 280 views
8

我想做一個極地「sypder」陰謀,但我有一些軸標籤的問題。 xaxis刻度標籤總是看起來最終位於y軸網格下面的一個圖層上(字母被網格線覆蓋,如下圖所示),我希望它們位於頂部。座標軸中隱藏的軸標籤?

我試着設置zorders但沒有成功。 如果我將繪製線的zorder設置爲2以上,它們會在軸和網格的頂部(以圖層的方式)...但我仍然希望標籤在圖的頂部可見。如果我將它們設置在2以下,那麼這些線條就會在網格下面。設置網格的zorder或ticks標籤似乎沒有效果。

這是我的嘗試:正如您可以看到網格的紅線最終位於文本「行業」之上,而網格的灰線位於下方。我想「產業」的,有兩個頂線和情節

import matplotlib.pyplot as plt 
import numpy as np 
import pandas as pd 
import random 

data = random.sample(range(100), 5) 
data[0] = 100 
data[3] = 50 
index = ['industry', 'residential', 'agriculture', 'transport', 'other'] 
df1 = pd.DataFrame(data, index=index, columns=['data']) 
df2 = pd.DataFrame(np.array(data)/2, index=index, columns=['data']) 

fig = plt.figure() 

ax = fig.add_subplot(111, projection="polar") 

ax.grid(True) 
ax.yaxis.grid(color='r') 
ax.xaxis.grid(color='#dddddd') 

for spine in ax.spines.values(): 
    spine.set_edgecolor('None') 

theta = np.arange(len(df1))/float(len(df1))*2.*np.pi 

l1, = ax.plot(theta, df1["data"], color="gold", marker="o", label=None, zorder=1) # , zorder = -3) 
l2, = ax.plot(theta, df2["data"], color='tomato', marker="o", label=None, zorder=1.1) #, zorder =-2) 

def _closeline(line): 
    x, y = line.get_data() 
    x = np.concatenate((x, [x[0]])) 
    y = np.concatenate((y, [y[0]])) 
    line.set_data(x, y) 
[_closeline(l) for l in [l1, l2]] 

ax.fill(theta, df1["data"], "gold", alpha=1, zorder=1) 
ax.fill(theta, df2["data"], 'tomato', alpha=1, zorder=1.1) 

ax.set_rlabel_position(216) 
ax.set_xticks(theta) 
ax.set_xticklabels(df2.index, fontsize=12)#, zorder=1) 

legend = plt.legend(handles=[l1,l2], labels =['first','second'], loc='lower right') 

plt.title("data [unit]", fontsize = 16, y = 1.2) 


plt.savefig('atlas//trial2.png', bbox_inches='tight', dpi = 300) 
plt.show() 

Here is my attempt: as you can see the red lines of the grid end up on top of the text 'industry' while the gray line of the grid stays below. I would like 'industry' to be on top of both line and of the plots

+1

我試過'zorder's _應該影響你的問題,無濟於事的幾種組合。這可能是一個類似於[這個]的錯誤(https://github.com/matplotlib/matplotlib/issues/5045)。極地地塊似乎總是比笛卡爾地塊更有問題。良好的問題的榮譽與可運行的例子! –

回答

0

嘛,這個解決了,即使它不是一個很好的回答我的問題。

在上面的腳本中,我用以下代碼替換ax.set_xticklabels(df2.index, fontsize=12)以替換軸標籤等效的文本。

for it in np.arange(len(theta)): 
    txt = ax.text(theta[it], maxy*1.1, index[it], va = 'center', ha = 'center', fontsize = 12) 

ax.set_xticklabels('') 

「標籤」現在位於軸和網格之上。

+0

如果您在回答問題時接受了答案,那麼您可能實際上沒有激勵其他人再次調查問題,因爲他們發現問題確實已經有了可接受的解決方案。 – ImportanceOfBeingErnest

+0

刪除的感謝,只是測試,如果我要得到的回分:) – esperluette

+0

不,如果你接受你自己的答案,積分會丟失。如果你接受別人的回答,獎勵給他們。 – ImportanceOfBeingErnest