用多个Y轴绘图

时间:2014-08-25 19:32:45

标签: python numpy matplotlib

使用ax.<plot_function>绘制图形上的对象时。我怎样才能"hold on"情节和 在同一个地块上渲染多个地块?

例如:

f = plt.figure(figsize=(13,6))
ax = f.add_subplot(111)

ax.plot(x1,y1)
ax.plot(x2,y2)

ax.plot(xN,yN)

我尝试过添加:

ax.hold(True)

在我开始绘图之前,但它不起作用。我认为问题在于它们都具有相同的y尺度,我只看到具有最大值范围的图。

如何在同一个图上绘制多个不同比例的一维阵列?有没有办法用不同的Y轴将它们绘制在一起?

1 个答案:

答案 0 :(得分:2)

问题中的代码应该没有问题:

import matplotlib.pyplot as plt
import numpy as np

# some data
x1 = np.linspace(0, 10, 100)
x2 = np.linspace(1, 11, 100)
xN = np.linspace(4, 5, 100)
y1 = np.random.random(100)
y2 = 0.5 + np.random.random(100)
yN = 1 + np.random.random(100)![enter image description here][1]

# and then the code in the question
f = plt.figure(figsize=(13,6))
ax = f.add_subplot(111)

ax.plot(x1,y1)
ax.plot(x2,y2)

ax.plot(xN,yN) 

# save the figure
f.savefig("/tmp/test.png")

创建:

enter image description here

应该是预期的。所以问题不在代码中。

您是否在shell中运行命令?哪一个? IPython的?

一个猜测:所有三个图都是图,但是图中的数据对于所有图都完全相同,并且它们相互重叠。