Style Plots using Matpotlib
0 1495
Using Built-in Styles in Matplotlib
import matplotlib.pyplot as plt
# List all available styles
print(plt.style.available)
# Apply a specific style
plt.style.use('ggplot')
# Sample plot
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 8, 6]
plt.plot(x, y)
plt.title("Styled Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Customizing Line Style and Width
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y, linestyle='--', linewidth=2, color='purple')
plt.title("Dashed Sine Wave")
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.grid(True)
plt.show()
Temporarily Applying a Style
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
y = np.cos(x)
with plt.style.context('seaborn-darkgrid'):
plt.plot(x, y, marker='o', color='green')
plt.title("Cosine Wave with Temporary Style")
plt.xlabel("Angle")
plt.ylabel("Cosine Value")
plt.show()
Conclusion
Styling plots using Matplotlib enhances the visual clarity and professionalism of your charts. Whether you're using built-in themes, customizing line properties, or applying temporary styles, Matplotlib offers powerful options to present your data in the most effective way.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments