MatPlotLib在整个图中所占的y [英] MatPlotLib share y across entire figure

查看:26
本文介绍了MatPlotLib在整个图中所占的y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用 Pandas 和 MatPlotLib 生成的图的示例.

Here is an example of a plot I am generating using Pandas and MatPlotLib.

请注意,即使我在代码中声明了sharey = True,y轴也仅在每一行之间共享.这对我没有太大帮助,因为我需要将所有图相互比较.

Please note that even though I stated sharey = True in the code, the y-Axis is only shared across each row.This isn't much help to me, as I need to compare all plots against each other.

如何仅将一个轴用于整个图?理想情况下,我还希望为每个图重复该轴.

How can I use just one axis for the entire plot? I'd also ideally want that axis repeated for each plot.

谢谢!

    for field in chosenFields:
        for dataID in dataIDs:

            fig = plt.figure()
            subplots = [fig.add_subplot(rows, cols, subplot) for
                        subplot in range(1, len(fileNames) + 1)]

            for subplot, plot, fileName in zip(subplots, plots, fileNames):

                graphData = Build_Graphs.prepareOutputGraph(plot[0],
                                                            field,
                                                            dataID,
                                                            batchName,
                                                            segmentName)

                haveLegend = True if len(graphData.columns) < 12 else False
                subplt = graphData.plot(ax = subplot,
                                        legend = haveLegend,
                                        title = fileName,
                                        sharey = True)

                Build_Graphs.labelGraph(subplt, field, dataID, batchName, segmentName)

            plt.get_current_fig_manager().window.showMaximized()

            writeOutput(outputDirectory, field, dataID, graphData)

            plt.show()

推荐答案

为使每个绘图重复获得相同的轴范围,您可以从所有现有的 get_ylim 中使用全局最小值/最大值设置所有轴,

In order to get the same axis range repeated for each plot, you can get_ylim from all existing and use global min/max to set all the axes,

import numpy as np
import matplotlib.pyplot as plt

#Setup dummy data
fig, subplots = plt.subplots(2,3)
x = np.linspace(0,2.*np.pi,1000)
[sp.plot(x,np.sin(x)*(10*np.random.randn(1))) for sp in subplots.reshape(-1)]

#Get global minimum and maximum y values accross all axis
ygmin = 0.; ygmax = 0.
for sp in subplots.reshape(-1):
    ymin, ymax = sp.get_ylim()
    ygmin = min(ygmin,ymin)
    ygmax = max(ygmax,ymax)

#Set same axis for all subplots
[sp.set_ylim((ygmin,ygmax)) for sp in subplots.reshape(-1)]

plt.show()

正如 paulH 所建议的,这也可以通过 sharey=True 完成作为 plt.subplots 的一部分.但是,默认情况下,除了第一个轴以外,y轴均不显示任何其他内容,因此您需要告诉 matplotlib 再次显示它们,

As suggested by paulH, this can also be done with sharey=True as part of plt.subplots. However, the y axis is hidden for anything but the first axis by default, so you need to tell matplotlib to show these again,

import numpy as np
import matplotlib.pyplot as plt

#Setup dummy data
fig, subplots = plt.subplots(2,3,sharey=True)
x = np.linspace(0,2.*np.pi,1000)
[sp.plot(x,np.sin(x)*(10*np.random.randn(1))) for sp in subplots.reshape(-1)]

#Show axis on all subplots
[plt.setp(sp.get_yticklabels(), visible=True) for sp in subplots.reshape(-1)]

plt.show()

您还可以指定 sharey="col"sharey="row" 分别共享列或行的轴.

You can also specify sharey="col" or sharey="row" to share axes alone the column or row respectively.

这篇关于MatPlotLib在整个图中所占的y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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