簡體   English   中英

如何將軸標簽移動到matplotlib中的箭頭附近

[英]how to move axes labels to near the arrows in matplotlib

下面是繪制函數的代碼,我需要將“ X”和“ Y”標簽移到第一象限,通常將其放置在相應箭頭附近。 這是怎么做的?

import pylab as p
import numpy as n

from mpl_toolkits.axes_grid import axislines


def cubic(x) :
    return x**3 + 6*x


def set_axes():
    fig = p.figure(1)
    ax = axislines.SubplotZero(fig, 111)
    fig.add_subplot(ax)

    for direction in ['xzero', 'yzero']:
        ax.axis[direction].set_axisline_style('->', size=2)
        ax.axis[direction].set_visible(True)

    for direction in ['right', 'top', 'left', 'bottom']:
        ax.axis[direction].set_visible(False)

    ax.axis['xzero'].set_label('X')
    ax.axis['yzero'].set_label('Y')

    ax.axis['yzero'].major_ticklabels.set_axis_direction('right')
    ax.axis['yzero'].set_axislabel_direction('+')
    ax.axis['yzero'].label.set_rotation(-90)
    ax.axis['yzero'].label.set_va('center')


set_axes()

X = n.linspace(-15,15,100)
Y = cubic(X)

p.plot(X, Y)

p.xlim(-5.0, 5.0)
p.ylim(-15.0, 15.0)

p.xticks(n.linspace(-5, 5, 11, endpoint=True))
p.grid(True)

p.show()

通常,要更改軸的標簽位置(例如ax.xaxis ),可以執行axis.label.set_position(xy) 或者,您可以只設置一個坐標,例如'ax.xaxis.set_x(1)`。

在您的情況下,它將是:

ax['xzero'].label.set_x(1)
ax['yzero'].label.set_y(1)

但是, axislines (以及axisartistaxes_grid其他任何東西)是有些過時的模塊(這就是為什么axes_grid1存在的原因)。 在某些情況下,它不能正確地將事物分類。 因此,當我們嘗試設置標簽的x和y位置時,沒有任何變化!


一種快速的解決方法是使用ax.annotate將標簽放置在箭頭的末端。 但是,讓我們先嘗試以不同的方式制作圖(此后,我們將最終返回以annotate的方式)。


如今,最好使用新的棘刺功能來完成您要完成的任務。

將x和y軸設置為“歸零”很簡單:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

for spine in ['left', 'bottom']:
    ax.spines[spine].set_position('zero')

# Hide the other spines...  
for spine in ['right', 'top']:
    ax.spines[spine].set_color('none')

ax.axis([-4, 10, -4, 10])
ax.grid()

plt.show()

在此處輸入圖片說明

但是,我們仍然需要漂亮的箭頭裝飾。 這有點復雜,但是用適當的參數進行注釋只是兩個調用。

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

#-- Set axis spines at 0
for spine in ['left', 'bottom']:
    ax.spines[spine].set_position('zero')

# Hide the other spines...
for spine in ['right', 'top']:
    ax.spines[spine].set_color('none')

#-- Decorate the spins
arrow_length = 20 # In points

# X-axis arrow
ax.annotate('', xy=(1, 0), xycoords=('axes fraction', 'data'), 
            xytext=(arrow_length, 0), textcoords='offset points',
            arrowprops=dict(arrowstyle='<|-', fc='black'))

# Y-axis arrow
ax.annotate('', xy=(0, 1), xycoords=('data', 'axes fraction'), 
            xytext=(0, arrow_length), textcoords='offset points',
            arrowprops=dict(arrowstyle='<|-', fc='black'))

#-- Plot
ax.axis([-4, 10, -4, 10])
ax.grid()

plt.show()

在此處輸入圖片說明

(箭頭的寬度由文本大小(或arrowprops的可選參數)控制,因此,如果需要,可以指定size=16進行annotate以使箭頭稍微寬一些。)


在這一點上,最簡單的方法是只添加“ X”和“ Y”標簽作為注釋的一部分,盡管設置它們的位置也可以。

如果我們只是將標簽作為第一個參數進行注釋而不是空字符串(並稍稍更改對齊方式),我們將在箭頭的末端得到漂亮的標簽:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

#-- Set axis spines at 0
for spine in ['left', 'bottom']:
    ax.spines[spine].set_position('zero')

# Hide the other spines...
for spine in ['right', 'top']:
    ax.spines[spine].set_color('none')

#-- Decorate the spins
arrow_length = 20 # In points

# X-axis arrow
ax.annotate('X', xy=(1, 0), xycoords=('axes fraction', 'data'), 
            xytext=(arrow_length, 0), textcoords='offset points',
            ha='left', va='center',
            arrowprops=dict(arrowstyle='<|-', fc='black'))

# Y-axis arrow
ax.annotate('Y', xy=(0, 1), xycoords=('data', 'axes fraction'), 
            xytext=(0, arrow_length), textcoords='offset points',
            ha='center', va='bottom',
            arrowprops=dict(arrowstyle='<|-', fc='black'))

#-- Plot
ax.axis([-4, 10, -4, 10])
ax.grid()

plt.show()

在此處輸入圖片說明

只需做一點點的工作(直接訪問書脊的變換),就可以泛化使用注釋來處理任何類型的書脊對齊方式(例如“掉落”的脊椎等)。

無論如何,希望能有所幫助。 如果願意,您也可以使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM