刻度线标签与自定义刺轴重叠;做一个“数学”人物

时间:2018-07-14 19:32:59

标签: matplotlib

我想创建一个mathy figure,在这里您可以实际绘制x轴和y轴。

function mathy_figure() # sorry this is PyPlot in Julia but works the same
    fig = plt.figure(figsize=(5, 5))
    ax = fig[:add_subplot](1, 1, 1)

    # Move left y-axis and bottom x-axis to center, passing through (0,0)
    ax[:spines]["left"][:set_position]("zero")
    ax[:spines]["bottom"][:set_position]("zero")

    # Eliminate upper and right axes
    ax[:spines]["right"][:set_color]("none")
    ax[:spines]["top"][:set_color]("none")
    return fig, ax
end

我什么也没做,即mathy_figure()

enter image description here

但是对于实际绘图,x轴和y轴重叠于棘!我该怎么做才能解决此问题?谢谢。

fig, ax = mathy_figure()
plt.plot(x, y, color="C3")
plt.plot(u, v, color="C1")
plt.axhline(y=N, color="k", linestyle="--")
plt.yticks([0, system.N], ["\$0\$", "\$N\$"])
plt.axvline(x=system.P₀, linestyle=":", color="k")
plt.xticks([0, system.P₀], ["\$0\$", "\$P_0\$"])
plt.tight_layout()
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:0)

以下代码段有效!现在0刻度不与轴相交!

# magic to prevent overlapping content
# see https://matplotlib.org/examples/pylab_examples/spine_placement_demo.html
ax[:spines]["left"][:set_smart_bounds](true)
ax[:spines]["bottom"][:set_smart_bounds](true)

ax[:xaxis][:set_ticks_position]("bottom")
ax[:yaxis][:set_ticks_position]("left")

对于axhline(),我必须通过xmin=0.05以防止与ytick标签“ N”重叠