YouTip LogoYouTip

Matplotlib Ref Text Advanced

Matplotlib Fig-level Text and Annotation Functions | Beginner Tutorial * * Matplotlib Reference Documentation](#) In addition to text() and annotate(), Matplotlib also provides Figure-level text functions, table addition, and arrow drawing. ## Function Overview | Function | Description | | --- | --- | | figtext() | Add text at Figure level (not Axes) | | figlegend() | Add legend at Figure level (across Axes) | | table() | Add table in Axes | | arrow() | Add simple arrows | * * * ## figtext() - Figure-level Text matplotlib.pyplot.figtext(x, y, s, fontdict=None, **kwargs) Similar to text(), but the coordinate system is Figure coordinates (0-1), not data coordinates. ## Example import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots(figsize=(8,5)) ax.plot([0,1],[0,1],'steelblue') ax.set_title('Axes-level (text vs figtext)') # Axes-level text (data coordinates) ax.text(0.3,0.7,'Axes text (data coords)', fontsize=12, color='blue') # Figure-level text (Figure relative coordinates 0-1) plt.figtext(0.5,0.02, 'Figure text at bottom center (Figure coords)', ha='center', fontsize=10, color='gray') plt.figtext(0.02,0.95, 'Top left', ha='left', va='top', fontsize=12, fontweight='bold', color='red') plt.show() * * * ## figlegend() - Figure-level Legend matplotlib.pyplot.figlegend(*args, **kwargs) When multiple Axes need to share a legend, figlegend() can place the legend at the Figure level. ## Example import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,10,100) fig,(ax1, ax2)= plt.subplots(1,2, figsize=(10,4), layout='constrained') # Each Axes draws the same two curves, but does not add its own legend ax1.plot(x, np.sin(x),'b-', label='sin(x)') ax1.plot(x, np.cos(x),'r--', label='cos(x)') ax1.set_title('Plot 1') ax2.plot(x, np.sin(x),'b-', label='sin(x)') ax2.plot(x, np.cos(x),'r--', label='cos(x)') ax2.set_title('Plot 2') # Figure-level legend (placed outside the Figure at the top) fig.legend(loc='outside upper center', ncol=2, fontsize=11, frameon=True) plt.show() * * * ## table() - Add Table in Axes matplotlib.pyplot.table(cellText=None, cellColours=None, cellLoc='right', colWidths=None, rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center', loc='bottom', bbox=None, edges='closed', **kwargs) ## Example import matplotlib.pyplot as plt import numpy as np # Data to display data =[[85,90,78,92], [76,88,82,85], [90,93,88,95]] row_labels =['Student A','Student B','Student C'] col_labels =['Math','English','Science','History'] fig, ax = plt.subplots(figsize=(8,4), layout='constrained') # Draw comparison chart x = np.arange(len(col_labels)) for i,(row, label)in enumerate(zip(data, row_labels)): ax.plot(x, row,'o-', label=label, markersize=8) ax.set_xticks(x) ax.set_xticklabels(col_labels) ax.set_title('Student Scores') ax.legend() ax.grid(True, alpha=0.3) # Add table below the chart table = ax.table(cellText=data, rowLabels=row_labels, colLabels=col_labels, cellLoc='center', loc='bottom', bbox=[0, -0.35,1,0.25])# [left, bottom, width, height] plt.show() * * * ## arrow() - Add Arrows matplotlib.pyplot.arrow(x, y, dx, dy, **kwargs) arrow() is a shortcut for adding simple arrows. For complex arrow annotations, using annotate() is recommended. ## Example import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots(figsize=(7,5), layout='constrained') # Draw coordinate system ax.spines[['left','bottom']].set_position(('data',0)) ax.spines[['top','right']].set_visible(False) ax.set_xlim(-1,5) ax.set_ylim(-1,5) # Various arrows ax.arrow(0,0,3,0, head_width=0.2, head_length=0.2, fc='red', ec='red', label='Right 3 units') ax.arrow(0,0,0,3, head_width=0.2, head_length=0.2, fc='blue', ec='blue', label='Up 3 units') ax.arrow(0,0,2,2, head_width=0.2, head_length=0.2, fc='green', ec='green', label='Diagonal') ax.set_title('arrow() - Simple Arrows') ax.legend() ax.grid(True, alpha=0.3) plt.show() [![Image 2: Matplotlib Reference Documentation](#) Matplotlib Reference Documentation](#)
← Matplotlib Ref ToolsMatplotlib Ref Spectral β†’