如何在图形中间绘制轴?
How to draw axis in the middle of the figure?
我想在 matplotib 中绘制一个图形,其中轴显示在绘图本身而不是侧面
我试过下面的代码from here:
import math
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
a = []
for item in x:
a.append(1/(1+math.exp(-item)))
return a
x = np.arange(-10., 10., 0.2)
sig = sigmoid(x)
plt.plot(x,sig)
plt.show()
以上代码显示如下图:
我想画的是下面的东西(image来自维基百科)
这个question描述了一个类似的问题,但是它在中间画了一条参考线但是没有轴。
一种方法是使用 spines:
import math
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
a = []
for item in x:
a.append(1/(1+math.exp(-item)))
return a
x = np.arange(-10., 10., 0.2)
sig = sigmoid(x)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# Move left y-axis and bottim x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# Show ticks in the left and lower axes only
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.plot(x,sig)
plt.show()
显示:
基本上,我想对已接受的答案发表评论(但我的代表不允许这样做)。
的使用
ax.spines['bottom'].set_position('center')
绘制 x-axes 使其在其中心与 y-axes 相交。在不对称 ylim 的情况下,这意味着 x-axis 不通过 y=0。 Jblasco 的答案有这个缺点,交点在 y=0.5(ymin=0.0 和 ymax=1.0 之间的中心)
但是,原始问题的参考图的轴在 0.0 处相互相交(这在某种程度上是常规的或至少是常见的)。
要实现此行为,
ax.spines['bottom'].set_position('zero')
必须使用。
请参见以下示例,其中 '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')
但零不在绘制的 y-range 内,则轴显示在更接近零的绘图边界处。
这个问题的标题是如何在中间画出脊椎,接受的答案正是这样做的,但是你们画的是 sigmoid 函数,它通过 y=0.5。所以我认为你想要的是根据你的数据居中的书脊。 Matplotlib 为 (see documentation)
提供了脊柱位置 data
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()
看起来像这样 (Github):
您只需添加:
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[:].set_position('center')
您可能会发现在 3 行中完成它会更简洁:
ax = plt.gca()
ax.spines[['top', 'right']].set_visible(False)
ax.spines[['left', 'bottom']].set_position('center')
参见文档 here。
使用 pip freeze
检查您的 matplotlib 版本并使用 pip install -U matplotlib
.
更新它
我想在 matplotib 中绘制一个图形,其中轴显示在绘图本身而不是侧面
我试过下面的代码from here:
import math
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
a = []
for item in x:
a.append(1/(1+math.exp(-item)))
return a
x = np.arange(-10., 10., 0.2)
sig = sigmoid(x)
plt.plot(x,sig)
plt.show()
以上代码显示如下图:
我想画的是下面的东西(image来自维基百科)
这个question描述了一个类似的问题,但是它在中间画了一条参考线但是没有轴。
一种方法是使用 spines:
import math
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
a = []
for item in x:
a.append(1/(1+math.exp(-item)))
return a
x = np.arange(-10., 10., 0.2)
sig = sigmoid(x)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# Move left y-axis and bottim x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# Show ticks in the left and lower axes only
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.plot(x,sig)
plt.show()
显示:
基本上,我想对已接受的答案发表评论(但我的代表不允许这样做)。
的使用ax.spines['bottom'].set_position('center')
绘制 x-axes 使其在其中心与 y-axes 相交。在不对称 ylim 的情况下,这意味着 x-axis 不通过 y=0。 Jblasco 的答案有这个缺点,交点在 y=0.5(ymin=0.0 和 ymax=1.0 之间的中心) 但是,原始问题的参考图的轴在 0.0 处相互相交(这在某种程度上是常规的或至少是常见的)。 要实现此行为,
ax.spines['bottom'].set_position('zero')
必须使用。 请参见以下示例,其中 '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')
但零不在绘制的 y-range 内,则轴显示在更接近零的绘图边界处。
这个问题的标题是如何在中间画出脊椎,接受的答案正是这样做的,但是你们画的是 sigmoid 函数,它通过 y=0.5。所以我认为你想要的是根据你的数据居中的书脊。 Matplotlib 为 (see documentation)
提供了脊柱位置 dataimport 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()
看起来像这样 (Github):
您只需添加:
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[:].set_position('center')
您可能会发现在 3 行中完成它会更简洁:
ax = plt.gca()
ax.spines[['top', 'right']].set_visible(False)
ax.spines[['left', 'bottom']].set_position('center')
参见文档 here。
使用 pip freeze
检查您的 matplotlib 版本并使用 pip install -U matplotlib
.