I want to draw a figure in matplotib where the axis are displayed within the plot itself not on the side
I have tried the following code from
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()
The above code displays the figure like this
What I would like to draw is something as follows (Image from Wiki)
Looked at this question, it draws a reference line in the middle but no axis.
Any help would be great!
Thanks
Basically, I want to comment on the accepted answer (but my rep doesn't allow that). The use of
draws the x-axes such that it intersect the y-axes in its center. In case of asymmetric ylim this means that x-axis passes NOT through y=0. Jblasco's answer has this drawback, the intersect is at y=0.5 (the center between ymin=0.0 and ymax=1.0) However, the reference plot of the original question has axes that intersect each other at 0.0 (which is somehow conventional or at least common). To achieve this behaviour,
has to be used. See the following example, where 'zero' makes the axes intersect at 0.0 despite asymmetrically ranges in both x and y.
Final remark: If
ax.spines['bottom'].set_position('zero')
is used but zerois not within the plotted y-range, then the axes is shown at the boundary of the plot closer to zero.The title of this question is how to draw the spine in the middle and the accepted answer does exactly that but what you guys draw is the sigmoid function and that one passes through y=0.5. So I think what you want is the spine centered according to your data. Matplotlib offers the spine position data for that (see documentation)
Looks like this (Github):
One way to do it is using spines:
shows: