简体   繁体   English

使用多个Y轴绘图

[英]Plotting with multiple Y-axes

When using ax.<plot_function> for plotting objects on a figure. 使用ax.<plot_function>在图形上绘制对象时。 How can I "hold on" the plot and render multiple plots on the same plot? 如何"hold on"绘图并在同一绘图上渲染多个绘图?

For example: 例如:

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

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

ax.plot(xN,yN)

I have tried adding: 我尝试添加:

ax.hold(True)

before I start plotting, but it doesn't work. 在我开始绘制之前,但是它不起作用。 I think the problem is that they all share the same y-scale and I only see the plot with the largest range of values. 我认为问题在于它们都共享相同的y比例,而我只看到值范围最大的图。

How can I plot multiple 1D arrays of different scales on the same plot? 如何在同一图上绘制多个不同比例的1D数组? Is there any way to plot them with different Y-axis next to each other? 有什么办法可以将它们的Y轴彼此相邻地绘制?

There should be nothing wrong with the code in the question: 问题中的代码应该没有任何问题:

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

Creates: 创建:

在此处输入图片说明

which should be pretty much what is expected. 这应该与预期的差不多。 So the problem is not in the code. 因此,问题不在代码中。

Are you running the commands in a shell? 您是否在外壳中运行命令? Which one? 哪一个? IPython? IPython?

One wild guess: All three plots are plot, but the data in the plots is exactly the same for all plots, and they overlap each other. 一个大胆的猜测:所有三个图都是图,但是图中的数据对于所有图都是完全相同的,并且它们相互重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM