【问题标题】:Add Overall X-Axis Descriptor to bottom of Pyplot Subplots将整体 X 轴描述符添加到 Pyplot 子图的底部
【发布时间】:2016-03-28 23:47:47
【问题描述】:

我有以下情节:

并且我需要在“整体”x 轴上放置一些文本——这样描述清楚地适用于沿列的三个图表中的每一个的 x 轴。

如何访问包含子图的表格的 x 轴标签? (或此处适用的任何对象)。

我使用以下代码创建了图表:

import matplotlib.pyplot as plt

f,(ax1,ax2,ax3) = plt.subplots(1,3, sharey=True)

#plotted my stuff
....

#now: how to add general label to entire subplot

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    你可以创建一个大的子图(比如'sub')并给它一个x-label。其他三个图形(sub1、sub2、sub3)可以作为第二个子图形添加到第一个图形中。现在,如果将 x-label 应用于原始的大子图形“sub”,它会显示为所有三个图形的公共 x-label。但是,您可以分别为三个数字添加不同的 y 标签。下面给出了一个显示相同内容的示例程序。

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.linspace(0.01,10,100)
    y1 = x
    y2 = np.log(x)
    y3 = np.exp(x)
    
    fig = plt.figure()
    sub = fig.add_subplot(111)
    sub1 = fig.add_subplot(131)
    sub2 = fig.add_subplot(132)
    sub3 = fig.add_subplot(133)
    
    sub1.plot(x, y1)
    sub2.plot(x, y2)
    sub3.plot(x, y3)
    
    sub.set_xlabel('functions of x')
    sub1.set_ylabel('$y=x$')
    sub2.set_ylabel('$y=sin(x)$')
    sub3.set_ylabel('$y=e^x$')
    
    plt.show()
    

    【讨论】:

      【解决方案2】:

      你可以试试fig.text。比如:

      fig.text(0.5, 0.04, 'My xlabel text', ha='center', va='bottom')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 2012-07-15
        • 1970-01-01
        • 1970-01-01
        • 2018-08-31
        相关资源
        最近更新 更多