坐标轴中隐藏的轴标签?

问题描述:

我想做一个极地“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)。极地地块似乎总是比笛卡尔地块更有问题。良好的问题的荣誉与可运行的例子! –

嘛,这个解决了,即使它不是一个很好的回答我的问题。

在上面的脚本中,我用以下代码替换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