matplotlib:不同比例的叠加图? [英] matplotlib: overlay plots with different scales?

查看:43
本文介绍了matplotlib:不同比例的叠加图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有以下代码:

So far I have the following code:

colors = ('k','r','b')
ax = []
for i in range(3):
    ax.append(plt.axes())
    plt.plot(datamatrix[:,0],datamatrix[:,i],colors[i]+'o')
    ax[i].set(autoscale_on=True)

对于每个轴的 autoscale_on=True 选项,我认为每个图都应该有自己的 y 轴限制,但看起来它们都共享相同的值(即使它们共享不同的轴).如何将它们设置为缩放以显示每个 datamatrix[:,i] 的范围(只是对 .set_ylim() 的显式调用?)我为上面可能需要的第三个变量 (datamatrix[:,2]) 创建了一个偏移 y 轴?谢谢大家.

With the autoscale_on=True option for each axis, I thought each plot should have its own y-axis limits, but it appears they all share the same value (even if they share different axes). How do I set them to scale to show the range of each datamatrix[:,i] (just an explicit call to .set_ylim()?) And also, how can I create an offset y-axis for the third variable (datamatrix[:,2]) that might be required above? Thanks all.

推荐答案

听起来你想要的是子情节......你现在所做的没有多大意义(或者我很困惑无论如何,您的代码片段......).

It sounds like what you're wanting is subplots... What you're doing now doesn't make much sense (Or I'm very confused by your code snippet, at any rate...).

尝试更像这样的事情:

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(nrows=3)

colors = ('k', 'r', 'b')
for ax, color in zip(axes, colors):
    data = np.random.random(1) * np.random.random(10)
    ax.plot(data, marker='o', linestyle='none', color=color)

plt.show()

如果你不想要子图,你的代码片段更有意义.

If you don't want subplots, your code snippet makes a lot more sense.

您正在尝试将三个轴叠加在一起.Matplotlib 认识到在图形上已经有一个精确大小和位置的子图,因此它每次都返回 相同 轴对象.换句话说,如果您查看列表 ax,您会发现它们都是同一个对象.

You're trying to add three axes right on top of each other. Matplotlib is recognizing that there's already a subplot in that exactly size and location on the figure, and so it's returning the same axes object each time. In other words, if you look at your list ax, you'll see that they're all the same object.

如果您真的想要这样做,则每次添加轴时都需要将 fig._seen 重置为空字典.不过,您可能真的不想这样做.

If you really want to do that, you'll need to reset fig._seen to an empty dict each time you add an axes. You probably don't really want to do that, however.

与其将三个独立的图放在一起,不如使用 twinx 代替.

Instead of putting three independent plots over each other, have a look at using twinx instead.

例如

import matplotlib.pyplot as plt
import numpy as np
# To make things reproducible...
np.random.seed(1977)

fig, ax = plt.subplots()

# Twin the x-axis twice to make independent y-axes.
axes = [ax, ax.twinx(), ax.twinx()]

# Make some space on the right side for the extra y-axis.
fig.subplots_adjust(right=0.75)

# Move the last y-axis spine over to the right by 20% of the width of the axes
axes[-1].spines['right'].set_position(('axes', 1.2))

# To make the border of the right-most axis visible, we need to turn the frame
# on. This hides the other plots, however, so we need to turn its fill off.
axes[-1].set_frame_on(True)
axes[-1].patch.set_visible(False)

# And finally we get to plot things...
colors = ('Green', 'Red', 'Blue')
for ax, color in zip(axes, colors):
    data = np.random.random(1) * np.random.random(10)
    ax.plot(data, marker='o', linestyle='none', color=color)
    ax.set_ylabel('%s Thing' % color, color=color)
    ax.tick_params(axis='y', colors=color)
axes[0].set_xlabel('X-axis')

plt.show()

这篇关于matplotlib:不同比例的叠加图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆