设置ylabel在matplotlib图中的位置 [英] Setting the position of the `ylabel` in a matplotlib graph

查看:4026
本文介绍了设置ylabel在matplotlib图中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib().

I am trying to recreate the look of figure below using matplotlib (source).

但是,我在放置ylabel时遇到问题.我希望它位于y轴的顶部,如图所示.我尝试用ax.yaxis.set_label_position()设置其位置,但这仅将leftright用作y轴.是否可以控制ylabel的位置,还是应该只使用ax.text并手动设置文本的位置?

However, I am having issues with the placement of the ylabel. I want it at the top of the y-axis, as it is on the figure. I have tried setting its position with ax.yaxis.set_label_position(), but this only accepts left or right for the y-axis. Is there an option to control the position of the ylabel, or should I just use ax.text and set the text's position manually?

事实证明,ax.set_ylabel(position=(x,y))设置标签相对于图形坐标的位置.但是,由于其水平旋转,因此标签在右边有点过分,并且position(x,y)似乎不接受负输入.有没有办法将标签向左移动一点?

As it turns out, the ax.set_ylabel(position=(x,y)) sets the position of the label relative to the graph coordinates. However, because of its horizontal rotation, the label is a little too much to the right, and position(x,y) does not seem to accept negative inputs. Is there a way to move the label a little to the left?

我在这里包括了用于生成图形骨架的代码,即使它很杂乱.

I include the code used to generate the skeleton of the figure here, even though it's rather messy.

import  matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r"\usepackage[charter]{mathdesign}"]
mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.size'] = 10

nb_procs   = np.array([1,     2,        4,      12,     24,    48,     96,     192,    384])

def adjust_spines(ax, spines):
    for loc, spine in ax.spines.items():
        if loc in spines:
            spine.set_position(('outward', 10))  # outward by 10 points
            spine.set_smart_bounds(True)
        else:
            spine.set_color('none')  # don't draw spine

    # turn off ticks where there is no spine
    if 'left' in spines:
        ax.yaxis.set_ticks_position('left')
    else:
        # no yaxis ticks
        ax.yaxis.set_ticks([])

    if 'bottom' in spines:
        ax.xaxis.set_ticks_position('bottom')
    else:
        # no xaxis ticks
        ax.xaxis.set_ticks([])

# -- We create the figure.
figPres = plt.figure(figsize=(3,1.75))
axPres  = figPres.add_subplot(111)

# -- We remove any superfluous decoration.
# Remove the axis decorations on the right and on the top.
axPres.spines['top'].set_visible(False)
axPres.spines['right'].set_visible(False)

# Make the remaining spines a light gray.
axPres.spines['bottom'].set_color('gray')
axPres.spines['left'].set_color('gray')

adjust_spines(axPres, ['left', 'bottom'])

# -- Set the x ticks.
axPres.set_xscale('log')
axPres.set_xlim((0.75,500))
axPres.set_xticks((nb_procs))
axPres.set_xticklabels( (r'1', r'2', r'4', r'12', r'24', r'48', r'96', r'192', r'384'), color='gray' )
axPres.xaxis.set_ticks_position('bottom')

for tic in axPres.xaxis.get_major_ticks():
  tic.tick1On = tic.tick2On = False

# -- Set the y ticks.
axPres.set_ylim((0,1))
axPres.set_yticks((0.0,0.5,1.0))
axPres.set_yticklabels((r'0', '', r'1'))
axPres.yaxis.set_ticks_position('left')
axPres.tick_params(axis='y', colors='gray')

#for tac in axPres.yaxis.get_major_ticks():
#  tac.tick1On = tac.tick2On = False
for toc in axPres.xaxis.get_minor_ticks():
  toc.tick1On = toc.tick2On = False

# -- Set the titles of the axes.
axPres.set_ylabel(r"Efficacit\'e", color='gray', rotation='horizontal')
axPres.yaxis.set_label_position('right')
axPres.set_xlabel(r"Nombre de processeurs", color='gray')

plt.show()

推荐答案

您可以使用 ax.yaxis.set_label_coords ,它确实接受负数.对于您的示例,我用set_label_position删除了该行,并添加了:

You can move the ylabel using ax.yaxis.set_label_coords, which does accept negative numbers. For your example, I removed the line with set_label_position, and added:

axPres.yaxis.set_label_coords(-0.1,1.02)

这篇关于设置ylabel在matplotlib图中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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