2015-07-22 35 views
9

Tôi muốn vẽ một con số trong matplotib nơi trục được hiển thị trong cốt truyện riêng của mình không ở bênVẽ trục ở giữa của figue trong python

Tôi đã thử đoạn mã sau 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() 

Đoạn mã trên sẽ hiển thị con số như thế này enter image description here

những gì tôi muốn vẽ một cái gì đó như sau (hình ảnh từ Wiki)

01.

enter image description here

Nhìn vào số question này, nó vẽ một đường tham chiếu ở giữa nhưng không có trục.

Mọi trợ giúp sẽ tuyệt vời!

Cảm ơn

Trả lời

14

Một cách để làm điều đó là sử dụng 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() 

show: enter image description here

+0

Cảm ơn, chính xác những gì tôi đang tìm kiếm! – Shan

6

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.

4

Tiêu đề của câu hỏi này là cách vẽ cột sống ở giữa và câu trả lời được chấp nhận thực hiện chính xác nhưng những gì bạn vẽ là hàm sigmoid và hàm này chuyển qua y = 0,5. Vì vậy, tôi nghĩ rằng những gì bạn muốn là cột sống tập trung theo dữ liệu của bạn. Matplotlib cung cấp vị trí cột sống dữ liệu cho rằng (see documentation)

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() 

Trông như thế này (Github):

enter image description here

Các vấn đề liên quan