3

我有这些数据结构:

  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 将它们绘制为线图

我想出的这段代码呈现如下图(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()

这里的问题看起来像是轴的缩放。但我不知道如何解决它。任何想法,建议如何正确显示曲率?

在此处输入图像描述

4

2 回答 2

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

产量

在此处输入图像描述

红线对应error_matrix[0, :],蓝线对应error_matrix[1, :]

另一种可能性是绘制比率error_matrix[0, :]/error_matrix[1, :]

于 2012-11-18T21:10:48.547 回答
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()

功能

顺便说一句,您不需要明确说明 2D 数组的维度。使用时error_matrix[i,:]error_matrix[i].

于 2012-11-19T04:48:01.867 回答