Python 如何在图中间画轴?

Python 如何在图中间画轴?,python,matplotlib,drawing,Python,Matplotlib,Drawing,我想在matplotib中绘制一个图形,其中轴显示在绘图本身中,而不是在侧面 我尝试了以下代码: 上面的代码显示如下图: 我想画的东西如下(来自维基百科) 这描述了一个类似的问题,但它在中间画了一条参考线,但没有轴。 < P>一个方法是使用: 显示: 基本上,我想对接受的答案发表评论(但我的代表不允许这样做)。 使用 ax.spines['bottom'].set_position('center') 绘制x轴,使其在其中心与y轴相交。对于不对称ylim,这意味着x轴不通过y=0。Jbla

我想在matplotib中绘制一个图形,其中轴显示在绘图本身中,而不是在侧面

我尝试了以下代码:

上面的代码显示如下图:

我想画的东西如下(来自维基百科)

这描述了一个类似的问题,但它在中间画了一条参考线,但没有轴。

< P>一个方法是使用:

显示:

基本上,我想对接受的答案发表评论(但我的代表不允许这样做)。 使用

ax.spines['bottom'].set_position('center')
绘制x轴,使其在其中心与y轴相交。对于不对称ylim,这意味着x轴不通过y=0。Jblasco的答案有一个缺点,相交点位于y=0.5(ymin=0.0和ymax=1.0之间的中心) 然而,原始问题的参考图的轴在0.0处相交(这在某种程度上是常规的或至少是常见的)。 为了实现这一行为

ax.spines['bottom'].set_position('zero')
必须使用。 请参见以下示例,其中“零”使轴在0.0处相交,尽管x和y的范围不对称

import numpy as np
import matplotlib.pyplot as plt

#data generation
x = np.arange(-10,20,0.2)
y = 1.0/(1.0+np.exp(-x)) # nunpy does the calculation elementwise for you


fig, [ax0, ax1] = plt.subplots(ncols=2, figsize=(8,4))

# Eliminate upper and right axes
ax0.spines['top'].set_visible(False)
ax0.spines['right'].set_visible(False)
# Show ticks on the left and lower axes only
ax0.xaxis.set_tick_params(bottom='on', top='off')
ax0.yaxis.set_tick_params(left='on', right='off')

# Move remaining spines to the center
ax0.set_title('center')
ax0.spines['bottom'].set_position('center') # spine for xaxis 
#    - will pass through the center of the y-values (which is 0)
ax0.spines['left'].set_position('center')  # spine for yaxis 
#    - will pass through the center of the x-values (which is 5)

ax0.plot(x,y)


# Eliminate upper and right axes
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
# Show ticks on the left and lower axes only (and let them protrude in both directions)
ax1.xaxis.set_tick_params(bottom='on', top='off', direction='inout')
ax1.yaxis.set_tick_params(left='on', right='off', direction='inout')

# Make spines pass through zero of the other axis
ax1.set_title('zero')
ax1.spines['bottom'].set_position('zero')
ax1.spines['left'].set_position('zero')

ax1.set_ylim(-0.4,1.0)

# No ticklabels at zero
ax1.set_xticks([-10,-5,5,10,15,20])
ax1.set_yticks([-0.4,-0.2,0.2,0.4,0.6,0.8,1.0])

ax1.plot(x,y)

plt.show() 

最后备注:如果使用
ax.spines['bottom'].set_position('zero')
,但zero不在绘制的y范围内,然后在图的边界处显示接近于零的轴。

< P>这个问题的标题是如何在中间画脊柱,并且接受的答案确实是这样的,但是你们画的是乙状函数,一个穿过y=0.5。因此,我认为您需要的是根据数据居中的脊椎。Matplotlib提供该()的脊椎位置数据

看起来是这样的():

您只需添加

plt.axhline()
plt.axvline()
工作示例:

import matplotlib.pyplot as plt
import numpy as np

def f(x):
    return np.sin(x) / (x/100)

delte = 100
Xs = np.arange(-delte, +delte +1, step=0.01)
Ys = np.array([f(x) for x in Xs])
plt.axhline(color='black', lw=0.5)
plt.axvline(color='black', lw=0.5)
plt.plot(Xs, Ys)
plt.show()

如果使用
matplotlib>=3.4.2
,则可以使用Pandas语法,并且只能在一行中执行:

plt.gca().spines[:]设置位置(“中心”)
您可能会发现用三行代码来完成这项工作更为简洁:

ax=plt.gca()
ax.spines[['顶部','右侧]].设置为可见(假)
斧头刺['左','下']]。设置_位置('中心')
请参阅文档。

使用
pip freeze
检查matplotlib版本,并使用
pip install-U matplotlib
更新matplotlib版本,谢谢,这正是我想要的!最好使用“零”特殊位置而不是“中心”。事实上,在前者中,如果绘图与x轴不对称,则轴将不会穿过原点,而只会到达绘图的中心。例如,尝试使用此设置绘制ELU函数。这非常酷。如何消除重复的零标签?@madpysicast它们不是真正重复的:一个指定x坐标为0的位置,另一个指定y坐标为0的位置。尽管如此,您可以使用
ax.set_yticks([0.2,0.4,0.6,0.8,1.0])
删除一个轴,或者提供一个替代标记列表,用于一个不包含0的轴。谢谢-我注意到
的“数据”,0.0可以简化
'zero'
我可以选择哪个值位于
y轴的中心吗
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
    return 1 / (1 + np.exp(-x))
sigmoid = np.vectorize(sigmoid) #vectorize function
values=np.linspace(-10, 10) #generate values between -10 and 10
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

#spine placement data centered
ax.spines['left'].set_position(('data', 0.0))
ax.spines['bottom'].set_position(('data', 0.0))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

plt.plot(values, sigmoid(values))
plt.show()
plt.axhline()
plt.axvline()
import matplotlib.pyplot as plt
import numpy as np

def f(x):
    return np.sin(x) / (x/100)

delte = 100
Xs = np.arange(-delte, +delte +1, step=0.01)
Ys = np.array([f(x) for x in Xs])
plt.axhline(color='black', lw=0.5)
plt.axvline(color='black', lw=0.5)
plt.plot(Xs, Ys)
plt.show()