【问题标题】:plotting more than one plot with different axes types绘制具有不同轴类型的多个图
【发布时间】:2012-05-08 14:11:51
【问题描述】:

我有以下代码,但我希望它们在同一个图表上,但在不同的子图上。

创建不同轴的最简单方法是什么?

from pylab import *

figure(0)
x =1
y = 2
plot(x, y, marker ='^', linewidth=4.0)
xlabel('time (s)')
ylabel('cost ($)')
title('cost vs. time')

figure(1)

x = 4
y = 100
plot(x, y, marker ='^', linewidth=4.0)
xlabel('cost ($)')
ylabel('performance (miles/hr) ')
title('cost vs. time')


show()

【问题讨论】:

    标签: python python-3.x matplotlib python-2.7


    【解决方案1】:

    您可以使用pyplot.subplots。我正在使用推荐用于编程的pyplot api(参见:http://matplotlib.sourceforge.net/api/pyplot_api.html#pyplot

    import matplotlib.pyplot as plt
    
    fig, (ax0, ax1) = plt.subplots(ncols=2)
    x = 1
    y = 2
    ax0.plot(x, y, marker ='^')
    ax0.set_xlabel('time (s)')
    ax0.set_ylabel('cost ($)')
    ax0.set_title('Plot 1')
    
    x = 4
    y = 100
    ax1.plot(x, y, marker ='^')
    ax1.set_xlabel('cost ($)')
    ax1.set_ylabel('performance (miles/hr)')
    ax1.set_title('Plot 2')
    
    fig.suptitle('cost vs. time')
    plt.subplots_adjust(top=0.85, bottom=0.1, wspace=0.25)
    plt.show()
    

    【讨论】:

    • 太棒了,但是标记这些子图并给每个子图一个特定标题的命令是什么...谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多