Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Visualizing Data in Matplotlib: Exploring Different Plot Styles

3 min readOct 13, 2024

--

Understanding the Code: A Step-by-Step Explanation

import matplotlib.pyplot as plt
import numpy as np

print(plt.style.available)

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

plt.style.use('ggplot')

plt.plot(x, y1, label='Sine Wave', color='blue', linestyle='--', linewidth=2)
plt.plot(x, y2, label='Cosine Wave', color='green', linestyle='-', linewidth=2)

plt.savefig("Plots/fig_with_style_ggplot.png")
plt.clf()

plt.style.use('classic')

plt.plot(x, y1, label='Sine Wave', color='blue', linestyle='--', linewidth=2)
plt.plot(x, y2, label='Cosine Wave', color='green', linestyle='-', linewidth=2)

plt.savefig("Plots/fig_with_style_classic.png")
plt.clf()

plt.style.use('dark_background')

plt.plot(x, y1, label='Sine Wave', color='blue', linestyle='--', linewidth=2)
plt.plot(x, y2, label='Cosine Wave', color='green', linestyle='-', linewidth=2)

plt.savefig("Plots/fig_with_style_dark-background.png")
plt.clf()

Importing Required Libraries

import matplotlib.pyplot as plt
import numpy as np
  • Matplotlib.pyplot: This module from Matplotlib provides a MATLAB-like interface for plotting. We import it as plt for convenience.
  • NumPy: It is a fundamental package for numerical computations in Python. We import it as np for easy access to its functions.

Displaying Available Plot Styles

print(plt.style.available)
  • This line prints a list of all the predefined styles available in Matplotlib. These styles help in customizing the appearance of your plots with ease.

Creating Data for Plotting

x = np.linspace(0, 10, 100)

y1 = np.sin(x)
y2 = np.cos(x)
  • Generating Data Points: The np.linspace function creates 100 evenly spaced values between 0 and 10. This array is stored in the variable x.
  • Computing Functions: We calculate the sine and cosine values for each point in x, storing them in y1 and y2, respectively.

Plotting with the ‘ggplot’ Style

plt.style.use('ggplot')

plt.plot(x, y1, label='Sine Wave', color='blue', linestyle=' - ', linewidth=2)
plt.plot(x, y2, label='Cosine Wave', color='green', linestyle='-', linewidth=2)

plt.savefig("Plots/fig_with_style_ggplot.png")
plt.clf()
  • Setting the Style: The code uses the ggplot style to give the plot a polished, grid-like appearance inspired by the ggplot2 package in R.
  • Plotting the Data: Two lines are plotted: the sine wave in blue with a dashed line style, and the cosine wave in green with a solid line style. The line width is set to 2 for both.
  • Saving and Clearing the Plot: The plot is saved as a PNG file, and then cleared from memory to allow for a new plot to be created.

Graph Output for ‘ggplot’ Style

Figure with style ‘ggplot’
Figure with style ‘ggplot’

Plotting with the ‘classic’ Style

plt.style.use('classic')

plt.plot(x, y1, label='Sine Wave', color='blue', linestyle=' - ', linewidth=2)
plt.plot(x, y2, label='Cosine Wave', color='green', linestyle='-', linewidth=2)

plt.savefig("Plots/fig_with_style_classic.png")
plt.clf()
  • Using the Classic Style: This style provides a more traditional look, similar to plots from earlier versions of Matplotlib.
  • Consistent Plotting Syntax: The data is plotted in the same manner as before, maintaining consistent labels, colors, and line styles.
  • File Handling: The plot is saved in the specified location, and the plotting space is cleared for the next plot.

Graph Output for ‘classic’ Style

Figure with style ‘classic’
Figure with style ‘classic’

Plotting with the ‘dark_background’ Style

plt.style.use('dark_background')

plt.plot(x, y1, label='Sine Wave', color='blue', linestyle=' - ', linewidth=2)
plt.plot(x, y2, label='Cosine Wave', color='green', linestyle='-', linewidth=2)

plt.savefig("Plots/fig_with_style_dark-background.png")
plt.clf()
  • Dark Background Style: This style changes the background to black, which enhances the contrast of the plotted lines and makes the visualization more striking.
  • Consistent Approach: The same data points and plot formatting are used, ensuring that the visual difference comes purely from the style.
  • Saving the Plot: The image is saved to the file and the plot is cleared from memory afterward.

Graph Output for ‘dark_background’ Style

Figure with style ‘dark background’
Figure with style ‘dark-background’

Conclusion

In this blog, we explored how to use different predefined styles in Matplotlib to customize the appearance of plots. By using the ‘ggplot’, ‘classic’, and ‘dark_background’ styles, we observed how each style uniquely transforms the presentation of the same data.

Get Someone’s stories in your inbox

Join Medium for free to get updates from this writer.

Experimenting with these styles allows you to create visualizations that are both informative and visually appealing, tailored to the specific needs of your projects.

For more details on the code implementation and to explore my other projects, you can check out my Github.

To stay updated with my progress on Matplotlib and other data visualization techniques, follow me on Twitter.

Thank you for being part of my journey in this 30th blog on Matplotlib!

--

--

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development