Về cơ bản, tôi muốn nhận xét về câu trả lời được chấp nhận (nhưng đại diện của tôi không cho phép điều đó). Việc sử dụng
ax.spines['bottom'].set_position('center')
vẽ các trục x sao cho giao nhau với trục y ở giữa. Trong trường hợp ylim không đối xứng, điều này có nghĩa là trục x đi qua NOT = y = 0. Câu trả lời của Jblasco có nhược điểm này, giao nhau là tại y = 0,5 (trung tâm giữa ymin = 0,0 và ymax = 1.0) Tuy nhiên, cốt truyện tham chiếu của câu hỏi ban đầu có các trục giao cắt với nhau tại 0.0 (bằng cách nào đó thông thường hoặc tại ít phổ biến nhất). Để đạt được hành vi này,
ax.spines['bottom'].set_position('zero')
phải được sử dụng. Xem ví dụ sau, trong đó 'số không' làm cho các trục giao nhau tại 0.0 bất chấp phạm vi bất đối xứng trong cả x và 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()
cuối cùng nhận xét: Nếu ax.spines['bottom'].set_position('zero')
được sử dụng nhưng zerois không thuộc y tầm vẽ trên biểu đồ, sau đó các trục được hiển thị ở ranh giới của cốt truyện gần gũi hơn với zero.
Cảm ơn, chính xác những gì tôi đang tìm kiếm! – Shan