【问题标题】:Python: displaying a line of text outside a matplotlib chartPython:在matplotlib图表外显示一行文本
【发布时间】:2015-09-23 14:36:00
【问题描述】:

我有一个由 matplotlib 库生成的矩阵图。我的矩阵大小是 256x256,我已经有一个图例和一个带有适当刻度的颜色条。由于我是stackoverflow的新手,我无法附加任何图像。无论如何,我使用这段代码来生成情节:

# Plotting - Showing interpolation of randomization
plt.imshow(M[-257:,-257:].T, origin='lower',interpolation='nearest',cmap='Blues', norm=mc.Normalize(vmin=0,vmax=M.max()))
title_string=('fBm: Inverse FFT on Spectral Synthesis')
subtitle_string=('Lattice size: 256x256 | H=0.8 | dim(f)=1.2 | Ref: Saupe, 1988 | Event: 50 mm/h, 15 min')
plt.suptitle(title_string, y=0.99, fontsize=17)
plt.title(subtitle_string, fontsize=9)
plt.show()

# Makes a custom list of tick mark intervals for color bar (assumes minimum is always zero)
numberOfTicks = 5
ticksListIncrement = M.max()/(numberOfTicks)
ticksList = []
for i in range((numberOfTicks+1)):
    ticksList.append(ticksListIncrement * i) 

cb=plt.colorbar(orientation='horizontal', format='%0.2f', ticks=ticksList) 
cb.set_label('Water depth [m]') 
plt.show()
plt.xlim(0, 255)
plt.xlabel('Easting (Cells)') 
plt.ylim(255, 0)
plt.ylabel('Northing (Cells)')

现在,由于我的字幕太长(此处报告的摘录中的第 3 行代码),它会干扰 Y 轴刻度,我不想要这个。相反,我想将字幕中报告的一些信息重新路由到一行文本,以放置在图像底部中心的颜色栏标签下方。 matplotlib 如何做到这一点?

抱歉无法附上图片。谢谢。

【问题讨论】:

    标签: python text matplotlib plot label


    【解决方案1】:

    通常,您会使用 annotate 来执行此操作。

    关键是将文本与 x 坐标放置在轴坐标中(因此它与轴对齐)和 y 坐标在图形坐标中(因此它位于图形的底部),然后添加偏移量点,所以它不在图的确切底部。

    作为一个完整的例子(我还展示了一个使用 extent kwarg 和 imshow 的例子,以防你不知道它):

    import numpy as np
    import matplotlib.pyplot as plt
    
    data = np.random.random((10, 10))
    
    fig, ax = plt.subplots()
    im = ax.imshow(data, interpolation='nearest', cmap='gist_earth', aspect='auto',
                   extent=[220, 2000, 3000, 330])
    
    ax.invert_yaxis()
    ax.set(xlabel='Easting (m)', ylabel='Northing (m)', title='This is a title')
    fig.colorbar(im, orientation='horizontal').set_label('Water Depth (m)')
    
    # Now let's add your additional information
    ax.annotate('...Additional information...',
                xy=(0.5, 0), xytext=(0, 10),
                xycoords=('axes fraction', 'figure fraction'),
                textcoords='offset points',
                size=14, ha='center', va='bottom')
    
    
    plt.show()
    

    其中大部分是复制与您的示例类似的内容。关键是annotate 电话。

    注释最常用于在相对于点 (xy) 的位置 (xytext) 上显示文本,并可选择用箭头连接文本和点,我们将在此处跳过。

    这有点复杂,让我们分解一下:

    ax.annotate('...Additional information...',  # Your string
    
                # The point that we'll place the text in relation to 
                xy=(0.5, 0), 
                # Interpret the x as axes coords, and the y as figure coords
                xycoords=('axes fraction', 'figure fraction'),
    
                # The distance from the point that the text will be at
                xytext=(0, 10),  
                # Interpret `xytext` as an offset in points...
                textcoords='offset points',
    
                # Any other text parameters we'd like
                size=14, ha='center', va='bottom')
    

    希望这会有所帮助。文档中的注释指南(introdetailed)作为进一步阅读非常有用。

    【讨论】:

    • 嗯,我喜欢这种快速直接的解决方案!非常感谢你。无需询问更多详细信息,因为所有 annotate 参数都清晰可见。 +1
    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2014-09-29
    相关资源
    最近更新 更多