Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to put a title for a curved line in Python Matplotlib?
To add a title to a curved line plot in Python Matplotlib, you use the plt.title() method after creating your plot. This is essential for making your visualizations clear and informative.
Steps
Set the figure size and adjust the padding between and around the subplots.
Create x and y data points such that the line would be a curve.
Plot the x and y data points using
plt.plot().Place a title for the curve plot using
plt.title()method.Display the figure using
plt.show()method.
Example
Here's a complete example showing how to create a curved line with a mathematical title ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create x and y data points for exponential curve
x = np.linspace(-1, 1, 50)
y = 2**x + 1
# Plot the curved line
line, = plt.plot(x, y, 'b-', linewidth=2)
# Add title with LaTeX formatting
plt.title("$y=2^x+1$", fontsize=14, fontweight='bold')
# Add axis labels for better visualization
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Output
The output displays an exponential curve with the mathematical formula as the title ?
Customizing the Title
You can customize the title appearance with additional parameters ?
import matplotlib.pyplot as plt
import numpy as np
# Create data for a sine curve
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
# Plot the curve
plt.plot(x, y, 'r-', linewidth=2)
# Customize title with various parameters
plt.title('Sine Wave Curve',
fontsize=16,
fontweight='bold',
color='darkblue',
pad=20)
plt.xlabel('x (radians)')
plt.ylabel('sin(x)')
plt.grid(True, alpha=0.3)
plt.show()
Title Parameters
| Parameter | Description | Example |
|---|---|---|
fontsize |
Size of title text | 12, 'large', 'x-large' |
fontweight |
Weight of title text | 'bold', 'normal' |
color |
Color of title text | 'red', 'blue', '#FF5733' |
pad |
Padding from plot | 20, 30 (in points) |
Conclusion
Use plt.title() to add descriptive titles to your curved line plots. You can use LaTeX formatting for mathematical expressions and customize appearance with fontsize, color, and other parameters for professional-looking visualizations.
