YouTip LogoYouTip

Matplotlib Ref Spans Vectors

\\n\\n\\n\\nMatplotlib Span Lines and Vector Field Functions | Tutorial\\n\\n\\n\\n* * *\\n\\n[![Image 1: Matplotlib Reference Documentation](https://example.com/images/up.gif) Matplotlib Reference Documentation](https://example.com/matplotlib/matplotlib-apiref.html)\\n\\nSpan functions are used to add reference lines and shaded bands that span the entire Axes in a chart; vector field functions are used to draw arrow plots and stream plots.\\n\\n## Function Overview\\n\\n| Function | Description |\\n| --- | --- |\\n| axhline() | Add a horizontal line spanning the Axes |\\n| axhspan() | Add a horizontal shaded band spanning the Axes |\\n| axvline() | Add a vertical line spanning the Axes |\\n| axvspan() | Add a vertical shaded band spanning the Axes |\\n| axline() | Add an infinite line passing through two points |\\n| quiver() | Draw vector field (arrow plot) |\\n| quiverkey() | Add scale legend to vector field |\\n| barbs() | Draw wind barbs (meteorological) |\\n| streamplot() | Draw stream plot |\\n\\n* * *\\n\\n## Span Functions\\n\\n### axhline() / axvline()\\n\\nmatplotlib.pyplot.axhline(y=0, xmin=0, xmax=1, **kwargs) matplotlib.pyplot.axvline(x=0, ymin=0, ymax=1, **kwargs)\\n### axhspan() / axvspan()\\n\\nmatplotlib.pyplot.axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs) matplotlib.pyplot.axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs)\\n### axline()\\n\\nmatplotlib.pyplot.axline(xy1, xy2=None, *, slope=None, **kwargs)\\n\\n## Example\\n\\nimport matplotlib.pyplot as plt\\n\\nimport numpy as np\\n\\nx = np.linspace(0,10,100)\\n\\n y = np.sin(x)\\n\\nfig,(ax1, ax2)= plt.subplots(1,2, figsize=(12,5),\\n\\n layout='constrained')\\n\\n# Left plot: span curve\\n\\n ax1.plot(x, y,'k-', alpha=0.7)\\n\\nax1.axhline(y=0, color='gray', linewidth=0.8)# y=0 Horizontal line\\n\\n ax1.axhline(y=0.5, color='green', linestyle='--')# y=0.5\\n\\n ax1.axvline(x=np.pi, color='red', linestyle='--')# x=Ο€\\n\\n ax1.axvspan(np.pi,2*np.pi, alpha=0.15,# Shaded band\\n\\n color='red', label='Phase 2')\\n\\n ax1.axhspan(0.5,1.0, xmin=0.6, xmax=1.0,# Local shaded band\\n\\n alpha=0.15, color='blue')\\n\\nax1.set_title('Span Functions')\\n\\n ax1.legend()\\n\\n# Right plot: axline defines an infinite line through two points\\n\\n ax2.plot([0,2,4],[0,2,3],'ro', label='Data points')\\n\\n ax2.axline((0,0),(2,2), color='blue', linestyle='--',\\n\\nlabel='Through (0,0) and (2,2)')\\n\\n ax2.axline((0,0), slope=1.5, color='green',\\n\\nlinestyle=':', label='From (0,0) with slope=1.5')\\n\\n ax2.set_xlim(-1,5)\\n\\n ax2.set_ylim(-1,5)\\n\\n ax2.set_title('axline()')\\n\\n ax2.legend()\\n\\n ax2.grid(True, alpha=0.3)\\n\\nplt.show()\\n\\n* * *\\n\\n## Vector Field Functions\\n\\n### quiver() - Vector Field\\n\\nmatplotlib.pyplot.quiver(*args, **kwargs)# X, Y, U, V: Grid coordinates and vector components# C: Optional color data\\n### streamplot() - Streamlines\\n\\nmatplotlib.pyplot.streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None, **kwargs)\\n### barbs() - Wind Barbs\\n\\nmatplotlib.pyplot.barbs(*args, **kwargs)# X, Y, U, V: Grid coordinates and wind components\\n\\n## Example\\n\\nimport matplotlib.pyplot as plt\\n\\nimport numpy as np\\n\\n# Create vector field data\\n\\n x = np.linspace(-3,3,15)\\n\\n y = np.linspace(-3,3,15)\\n\\n X, Y = np.meshgrid(x, y)\\n\\n# Vortex field\\n\\n U = -Y\\n\\n V = X\\n\\n# Velocity magnitude\\n\\n speed = np.sqrt(U**2 + V**2)\\n\\nfig,(ax1, ax2)= plt.subplots(1,2, figsize=(12,5),\\n\\n layout='constrained')\\n\\n# Left plot: quiver arrow plot\\n\\n q = ax1.quiver(X, Y, U, V, speed, cmap='viridis',\\n\\nscale=50, width=0.005)\\n\\n fig.colorbar(q, ax=ax1, label='Speed')\\n\\n ax1.set_title('quiver() - Arrow Field')\\n\\n ax1.set_aspect('equal')\\n\\n# Right plot: streamplot streamline plot\\n\\n strm = ax2.streamplot(x, y, U, V, color=speed,\\n\\n cmap='plasma', density=1.5, linewidth=1.5)\\n\\n fig.colorbar(strm.lines, ax=ax2, label='Speed')\\n\\n ax2.set_title('streamplot() - Streamlines')\\n\\n ax2.set_aspect('equal')\\n\\nplt.show()\\n\\n### barbs() Example\\n\\n## Example\\n\\nimport matplotlib.pyplot as plt\\n\\nimport numpy as np\\n\\n# Simulated wind field data\\n\\n x = np.linspace(0,10,10)\\n\\n y = np.linspace(0,8,8)\\n\\n X, Y = np.meshgrid(x, y)\\n\\n# Wind components (uniform eastward wind + random perturbation)\\n\\n U =10 + np.random.randn(*X.shape) * 3\\n\\n V = np.random.randn(*X.shape) * 5\\n\\nfig, ax = plt.subplots(figsize=(8,5), layout='constrained')\\n\\n# Wind barbs plot: barbs indicate wind speed, direction indicates wind direction\\n\\n ax.barbs(X, Y, U, V, length=6)\\n\\nax.set_title('barbs() - Wind Barbs (Meteorological)')\\n\\n ax.set_xlabel('Longitude')\\n\\n ax.set_ylabel('Latitude')\\n\\n ax.set_x
← Matplotlib Ref SubplotsMatplotlib Ref Savefig β†’