具有相同“设置"的matplotlib子图 [英] matplotlib subplots with same 'settings'

查看:67
本文介绍了具有相同“设置"的matplotlib子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以两种不同的格式绘制相同的数据:对数刻度和线性刻度.

I'm plotting the same data in two different formats: log scale and linear scale.

我基本上希望拥有完全相同的图,但是比例不同,一个在另一个顶部.

Basically I want to have exactly the same plot, but with different scales, one on the top of the other.

我现在拥有的是:

import matplotlib.pyplot as plt

# These are the plot 'settings'
plt.xlabel('Size')
plt.ylabel('Time(s)');
plt.title('Matrix multiplication')

plt.xticks(xl, rotation=30, size='small')
plt.grid(True)

# Settings are ignored when using two subplots

plt.subplot(211)
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')

plt.subplot(212)
plt.yscale('log')
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')

plt.subplot 之前的所有设置"都将被忽略.

All 'settings' before plt.subplot are ignored.

我可以让它按照我想要的方式工作,但我必须在每个子图声明后复制所有设置.

I can get this to work the way I want, but I have to duplicate all the settings after each subplot declaration.

有没有办法同时配置两个子图?

Is there a way to do configure both subplots at once?

推荐答案

plt.* 设置通常适用于 matplotlib 的 current 绘图;使用 plt.subplot ,您正在开始一个新图,因此设置不再适用于它.您可以通过 Axes 与绘图关联的对象(参见此处的示例),但是恕我直言,这在这里会过分杀人.相反,我建议将常见的样式"放入一个函数中,并按图调用它:

The plt.* settings usually apply to matplotlib's current plot; with plt.subplot, you're starting a new plot, hence the settings no longer apply to it. You can share labels, ticks, etc., by going through the Axes objects associated with the plots (see examples here), but IMHO this would be overkill here. Instead, I would propose putting the common "styling" into one function and call that per plot:

def applyPlotStyle():
    plt.xlabel('Size')
    plt.ylabel('Time(s)');
    plt.title('Matrix multiplication')

    plt.xticks(range(100), rotation=30, size='small')
    plt.grid(True)

plt.subplot(211)
applyPlotStyle()
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')

plt.subplot(212)
applyPlotStyle()
plt.yscale('log')
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')

另一方面,您可以通过将绘图命令提取到此类函数中来消除更多重复操作:

On a side note, you could root out more duplication by extracting your plot commands into such a function:

def applyPlotStyle():
    plt.xlabel('Size')
    plt.ylabel('Time(s)');
    plt.title('Matrix multiplication')

    plt.xticks(range(100), rotation=30, size='small')
    plt.grid(True)

def plotSeries():
    applyPlotStyle()
    plt.plot(xl, serial_full, 'r--')
    plt.plot(xl, acc, 'bs')
    plt.plot(xl, cublas, 'g^')

plt.subplot(211)
plotSeries()

plt.subplot(212)
plt.yscale('log')
plotSeries()

另一方面,例如,使用

On another side note, it might suffice to put the title at the top of the figure (instead of over each plot), e.g., using suptitle. Similary, it might be sufficient for the xlabel to only appear beneath the second plot:

def applyPlotStyle():
    plt.ylabel('Time(s)');

    plt.xticks(range(100), rotation=30, size='small')
    plt.grid(True)

def plotSeries():
    applyPlotStyle()
    plt.plot(xl, serial_full, 'r--')
    plt.plot(xl, acc, 'bs')
    plt.plot(xl, cublas, 'g^')

plt.suptitle('Matrix multiplication')
plt.subplot(211)
plotSeries()

plt.subplot(212)
plt.yscale('log')
plt.xlabel('Size')
plotSeries()

plt.show()

这篇关于具有相同“设置"的matplotlib子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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