2012-11-18 11 views
3

正確我有這些數據結構:如何獲得此線圖顯示了使用matplotlib

X axis values: 
delta_Array = np.array([1000,2000,3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]) 

    Y Axis values 
    error_matrix = 
[[ 24.22468454 24.22570421 24.22589308 24.22595919 24.22598979 
    24.22600641 24.22601644 24.22602294 24.2260274 24.22603059] 
    [ 28.54275713 28.54503017 28.54545119 28.54559855 28.54566676 
    28.54570381 28.54572615 28.54574065 28.5457506 28.54575771]] 

如何使用matplotlib和python

此代碼繪製出來的線圖,我想出了與呈現一個扁平線如下 圖(之三) I = 0

for i in range(error_matrix.shape[0]): 
    plot(delta_Array, error_matrix[i,:]) 

title('errors') 
xlabel('deltas') 
ylabel('errors') 
grid() 
show() 

這裏的問題看起來像被縮放軸。但我不知道如何解決它。任何想法,建議如何讓曲率正確顯示?

enter image description here

回答

3

你可以使用ax.twinx創建雙軸:

import matplotlib.pyplot as plt 
import numpy as np 

delta_Array = np.array([1000,2000,3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]) 

error_matrix = np.array(
    [[ 24.22468454, 24.22570421, 24.22589308, 24.22595919, 24.22598979, 24.22600641, 24.22601644, 24.22602294, 24.2260274, 24.22603059], 
    [ 28.54275713, 28.54503017, 28.54545119, 28.54559855, 28.54566676, 28.54570381, 28.54572615, 28.54574065, 28.5457506, 28.54575771]]) 


fig = plt.figure() 
ax = [] 
ax.append(fig.add_subplot(1, 1, 1)) 
ax.append(ax[0].twinx()) 
colors = ('red', 'blue') 

for i,c in zip(range(error_matrix.shape[0]), colors): 
    ax[i].plot(delta_Array, error_matrix[i,:], color = c) 
plt.show() 

產量

enter image description here

紅線對應error_matrix[0, :],藍色與error_matrix[1, :]

另一種可能性是繪製比率error_matrix[0, :]/error_matrix[1, :]

1

Matplotlib向你展示了正確的東西。如果你希望兩條曲線都在相同的y尺度上,那麼它們將是平坦的,因爲它們的差別遠大於它們之間的差異。如果你不介意不同的y尺度,那麼按照unutbu的建議。

如果要比較的功能之間的變化率,那麼我會建議由最高值正火每個:

import matplotlib.pyplot as plt 
import numpy as np 

plt.plot(delta_Array, error_matrix[0]/np.max(error_matrix[0]), 'b-') 
plt.plot(delta_Array, error_matrix[1]/np.max(error_matrix[1]), 'r-') 
plt.show() 

functions

順便說一下,你不」你需要明確你的二維數組的維數。當您使用error_matrix[i,:]時,它與error_matrix[i]相同。