![](/img/trans.png)
[英]Overlaying a lineCollection on a plot in matplotlib - how to get the two to line up.
[英]How to get this line plot to show up properly using 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将它们绘制为线图
我想出的这个代码呈现一条平线,如下图(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()
这里的问题看起来像是轴的缩放。 但我不知道如何解决它。 任何想法,建议如何正确显示曲率?
您可以使用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, :]
。
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]
相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.