如何使用matplotlib使这条线图正确显示

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

我想到的这段代码生成了一条平直的线,如下所示: 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()

这里的问题似乎是轴的缩放。但是我不确定如何解决它。有什么想法或建议可以让曲率正确显示吗?
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()

产量

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

顺便说一下,您不需要在2D数组的维度上进行明确说明。当您使用error_matrix[i,:]时,它与error_matrix[i]相同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接