如何在matplotlib中增加轴的箭头大小

2024-03-29 14:22:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用here中的示例。 enter image description here

我想增加x轴和y轴末端箭头的大小。我发现this

set_arrowstyle(“->,size=1.5”) set_arrowstyle(“->”, size=1.5)

所以接下来我试着:

ax.axis["xzero"].set_axisline_style("->",  size=5)

但这帮不了我。你知道吗


Tags: 示例sizeherestyle箭头axthisset
2条回答

size参数必须在引号内。在逗号后的引号外传递。使用以下命令

ax.axis[direction].set_axisline_style("-|>, size=4")

enter image description here

来自official website的完整代码

from mpl_toolkits.axisartist.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np

if 1:
    fig = plt.figure(1)
    ax = SubplotZero(fig, 111)
    fig.add_subplot(ax)

    for direction in ["xzero", "yzero"]:
        # adds arrows at the ends of each axis
        ax.axis[direction].set_axisline_style("-|>, size=4") # < - modified here

        # adds X and Y-axis from the origin
        ax.axis[direction].set_visible(True)

    for direction in ["left", "right", "bottom", "top"]:
        # hides borders
        ax.axis[direction].set_visible(False)

    x = np.linspace(-0.5, 1., 100)
    ax.plot(x, np.sin(x*np.pi))
    plt.show()

这个问题与jupyter笔记本及其内联后端的使用有关。因此,如果使用%matplotlib notebook后端,您将获得正确的输出。(为此,需要重新启动内核。)

%matplotlib notebook
from mpl_toolkits.axisartist.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np


fig = plt.figure(1)
ax = SubplotZero(fig, 111)
fig.add_subplot(ax)

for direction in ["xzero", "yzero"]:
    # adds arrows at the ends of each axis
    ax.axis[direction].set_axisline_style("-|>", size=5)

    # adds X and Y-axis from the origin
    ax.axis[direction].set_visible(True)

for direction in ["left", "right", "bottom", "top"]:
    # hides borders
    ax.axis[direction].set_visible(False)

x = np.linspace(-0.5, 1., 100)
ax.plot(x, np.sin(x*np.pi))

plt.show()

enter image description here

如果您想/需要使用%matplotlib inline后端,您可能需要还原一些设置,这样箭头就不会从图形中裁剪出来。你知道吗

  1. 创建png图形的默认设置是使用bbox_inches="tight"选项。这可以通过

    %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
    
  2. 默认地物大小、dpi和子地块参数are different。恢复这些可以通过

    plt.rcdefaults() 
    

由于Iypthon中的a bug,rcParameters不应设置在笔记本的第一个单元格中。你知道吗

因此

# Cell 1

%matplotlib inline
%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}

# Cell 2
import matplotlib.pyplot as plt
plt.rcdefaults() 
from mpl_toolkits.axisartist.axislines import SubplotZero
import numpy as np

fig = plt.figure(1)

ax = SubplotZero(fig, 111)
fig.add_subplot(ax)

for direction in ["xzero", "yzero"]:
    # adds arrows at the ends of each axis
    ax.axis[direction].set_axisline_style("-|>", size=5)

    # adds X and Y-axis from the origin
    ax.axis[direction].set_visible(True)

for direction in ["left", "right", "bottom", "top"]:
    # hides borders
    ax.axis[direction].set_visible(False)

x = np.linspace(-0.5, 1., 100)
ax.plot(x, np.sin(x*np.pi))

plt.show()

enter image description here

相关问题 更多 >