How to get this line drawing to appear correctly using matplotlib

advertisements

I have these data structures:

  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]]

How do I plot them as a line plot using matplotlib and python

This code I came up with renders a flat line as follows figure(3) i = 0

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

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

The problem here looks like is scaling of the axes. But im not sure how to fix it. Any ideas, suggestions how to get the curvature showing up properly?


You could use ax.twinx to create twin axes:

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

yields

The red line corresponds to error_matrix[0, :], the blue with error_matrix[1, :].

Another possibility is to plot the ratio error_matrix[0, :]/error_matrix[1, :].