在图中轴被隐藏的轴标签? [英] Axis label hidden by axis in plot?

查看:83
本文介绍了在图中轴被隐藏的轴标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制极坐标"sypder"图,但是轴标签存在一些问题. x轴刻度线似乎总是终止在y轴网格下方的图层上(字母被网格线覆盖,如下图所示),我希望它们位于顶部.

I am trying to make a polar 'sypder' plot but I am having some problems with the axis labels. The xaxis tick lables always seem to end up on a layer below the y axis grid (the letters are covered by the grid line, as shown in the figure below), I would like them on top.

我尝试设置zorders,但没有成功. 如果我将绘制线的zorder设置为2以上,它们的确会在轴和网格的顶部(就图层而言)...但是我仍然希望标签在图的顶部可见.如果将它们设置为2以下,则线会位于网格下方.设置网格的zorder或刻度标签似乎没有任何效果.

I tried setting the zorders but with no success. If I set the zorder of the plotted lines above 2 they do go on top (in terms of layer) of the axis and grid... but I still want the labels to be visible on top of the plot. If I set them below 2 the lines go below the grid. Setting the zorder of the grid or the ticks labels does not seem to have an effect.

这是我的尝试:正如您所看到的,网格的红线最终位于文本行业"的顶部,而网格的灰线则位于下方.我希望行业"同时位于情节和情节中

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

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

推荐答案

好吧,即使这不是一个好的答案,也可以解决我的问题.

Well, this solves my problem even though it is not a nice answer.

在上面的脚本中,我将ax.set_xticklabels(df2.index, fontsize=12)替换为以下内容,以将等效文本替换为轴标签.

In the script above I substitute ax.set_xticklabels(df2.index, fontsize=12) with the following to substitute to axis labels the equivalent text.

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

标签"现在位于轴和网格上方.

The 'labels' are now above the axis and the grids.

这篇关于在图中轴被隐藏的轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆