简体   繁体   English

在Matplotlib中具有独立缩放的多个重叠图

[英]Multiple overlapping plots with independent scaling in Matplotlib

I currently have code that calls matplotlib.pylab.plot multiple times to display multiple sets of data on the same screen, and Matplotlib scales each to the global min and max, considering all plots. 我目前有多次调用matplotlib.pylab.plot代码在同一屏幕上显示多组数据,并且考虑到所有图,Matplotlib将每个数据缩放到全局最小值和最大值。 Is there a way to ask it to scale each plot independently, to the min and max of that particular plot? 有没有办法要求它独立地缩放每个图,到特定图的最小值和最大值?

There's no direct support for this, but here's some code from a mailing list posting that illlustrates two independent vertical axes: 没有直接的支持,但是这里有一些来自邮件列表的代码,它会影响两个独立的垂直轴:

x=arange(10)
y1=sin(x)
y2=10*cos(x)

rect=[0.1,0.1,0.8,0.8]
a1=axes(rect)
a1.yaxis.tick_left()
plot(x,y1)
ylabel('axis 1')
xlabel('x')

a2=axes(rect,frameon=False)
a2.yaxis.tick_right()
plot(x,y2)
a2.yaxis.set_label_position('right')
ylabel('axis 2')
a2.set_xticks([])

I need something like this but wanted to create an example that you can copy and paste into the interactive shell and take a look at it. 我需要这样的东西,但想创建一个示例,您可以将其复制并粘贴到交互式shell中并查看它。 Here it is for those of you requiring a working solution: 这对于那些需要工作解决方案的人来说:

from numpy import arange
from math import sin, cos
import matplotlib.pyplot as plt

x = arange(10)
y1 = [sin(i) for i in x]
y2 = [10*cos(i) for i in x]

rect = [0.1, 0.1, 0.8, 0.8]
a1 = plt.axes(rect)  # Create subplot, rect = [left, bottom, width, height] in normalized (0, 1) units
a1.yaxis.tick_left()  # Use ticks only on left side of plot
plt.plot(x, y1)
plt.ylabel('axis 1')
plt.xlabel('x')

a2 = plt.axes(rect, frameon=False)  # frameon, if False, suppress drawing the figure frame
a2.yaxis.tick_right()
plt.plot(x, y2)
a2.yaxis.set_label_position('right')
plt.ylabel('axis 2')
a2.set_xticks([])

plt.show()

Tested and works in python 2.7.6, numpy 1.8.1, matpotlib 1.3.1. 测试并在python 2.7.6,numpy 1.8.1,matpotlib 1.3.1中工作。 I'm going to continue playing with it, looking for a neat way to work with overlaying date plots. 我将继续玩它,寻找一种巧妙的方式来处理覆盖的日期图。 I'll post back my findings. 我会回复我的发现。

Here is a solution using date plots, and I think its the most optimized solution using twinx() a short hand for adding a second y axis. 这是一个使用日期图的解决方案,我认为它是使用twinx()的最优化解决方案,用于添加第二个y轴。

import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime
import numpy
numpy.random.seed(0)
t = md.drange(datetime.datetime(2012, 11, 1),
            datetime.datetime(2014, 4, 01),
            datetime.timedelta(hours=1))  # takes start, end, delta
x1 = numpy.cumsum(numpy.random.random(len(t)) - 0.5) * 40000
x2 = numpy.cumsum(numpy.random.random(len(t)) - 0.5) * 0.002
fig = plt.figure()
ax1 = fig.add_subplot(111)
fig.suptitle('a title', fontsize=14)
fig.autofmt_xdate()
plt.ylabel('axis 1')
plt.xlabel('dates')
ax2 = ax1.twinx()
ax1.plot_date(t, x1, 'b-', alpha=.65)
ax2.plot_date(t, x2, 'r-', alpha=.65)
plt.ylabel('axis 2')
plt.show()

From the docs, matplotlib.pyplot.twinx(ax=None) Make a second axes that shares the x-axis. 从文档中,matplotlib.pyplot.twinx(ax = None)创建共享x轴的第二个轴。 The new axes will overlay ax (or the current axes if ax is None). 新轴将覆盖ax(如果ax为None,则覆盖当前轴)。 The ticks for ax2 will be placed on the right, and the ax2 instance is returned. ax2的刻度将放在右侧,并返回ax2实例。 More here . 更多这里

This is how you create a single plot (add_subplot(1,1,1)) and limit the scale on the y-axes. 这是您创建单个图(add_subplot(1,1,1))并限制y轴上的比例的方法。

myFig = figure()
myPlot = self.figure.add_subplot(1,1,1)
myPlot.plot([1,2,3,4,5], [5,4,3,2,1], '+r')
myPlot.set_ylim(1,5) # Limit y-axes min 1, max 5

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

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