Matplotlib

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It provides a wide range of tools for plotting 2D and 3D data, including line plots, scatter plots, bar charts, histograms, and more. Matplotlib is built on NumPy and is designed to work seamlessly with other Python libraries like Pandas and Scikit-Learn.

Some key features of Matplotlib include:

  1. Plotting Functions: Matplotlib provides a variety of plotting functions for creating different types of plots, such as plot() for line plots, scatter() for scatter plots, bar() for bar charts, and hist() for histograms. These functions allow you to customize the appearance of your plots, including colors, labels, and legends.

  2. Subplots and Layouts: Matplotlib allows you to create multiple plots within a single figure using subplots. You can arrange these subplots in a grid or customize their layout to create more complex visualizations. This is particularly useful when you want to compare multiple datasets or visualize different aspects of your data simultaneously.

  3. Customization: Matplotlib offers extensive customization options for fine-tuning the appearance of your plots. You can adjust the colors, fonts, tick labels, axis labels, and titles to create visually appealing and informative visualizations. You can also save your plots in various formats, such as PNG, JPEG, or SVG, for use in presentations or publications.

  4. Interactive Plots: Matplotlib supports interactive plots that allow users to zoom, pan, and hover over data points to reveal additional information. This is particularly useful when working with large datasets or when you want to provide an interactive experience for your audience.

Here’s a simple example of how to create a line plot using Matplotlib:

import matplotlib.pyplot as plt
import numpy as np

# Generate some data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create a figure and axis
fig, ax = plt.subplots()

# Plot the data
ax.plot(x, y)

# Set labels and title
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Line Plot Example')

# Show the plot
plt.show()

In this example, we first import the matplotlib.pyplot module and the numpy library. We then generate some sample data using numpy.linspace() and numpy.sin(). We create a figure and axis using plt.subplots(), plot the data using ax.plot(), set labels and a title using ax.set_xlabel(), ax.set_ylabel(), and ax.set_title(), and finally display the plot using plt.show().

Some more examples

import matplotlib.pyplot as plt
import numpy as np
  1. plt.plot(): Create a line plot

    x = np.linspace(0, 10, 100)
    plt.plot(x, np.sin(x))
    plt.show()
    
  2. plt.scatter(): Create a scatter plot

    x = np.random.rand(50)
    y = np.random.rand(50)
    plt.scatter(x, y)
    plt.show()
    
  3. plt.bar(): Create a bar plot

    x = ['A', 'B', 'C', 'D']
    y = [3, 7, 2, 5]
    plt.bar(x, y)
    plt.show()
    
  4. plt.hist(): Create a histogram

    data = np.random.randn(1000)
    plt.hist(data, bins=30)
    plt.show()
    
  5. plt.boxplot(): Create a box plot

    data = [np.random.normal(0, std, 100) for std in range(1, 4)]
    plt.boxplot(data)
    plt.show()
    
  6. plt.imshow(): Display an image

    img = np.random.rand(10, 10)
    plt.imshow(img, cmap='viridis')
    plt.colorbar()
    plt.show()
    
  7. plt.contour(): Create a contour plot

    x = np.linspace(-3, 3, 100)
    y = np.linspace(-3, 3, 100)
    X, Y = np.meshgrid(x, y)
    Z = np.sin(X) + np.cos(Y)
    plt.contour(X, Y, Z)
    plt.show()
    
  8. plt.subplot(): Create multiple subplots

    plt.subplot(2, 2, 1)
    plt.plot(x, np.sin(x))
    plt.subplot(2, 2, 2)
    plt.plot(x, np.cos(x))
    plt.show()
    
  9. plt.figure(): Create a new figure

    plt.figure(figsize=(10, 6))
    plt.plot(x, np.sin(x))
    plt.show()
    
  10. plt.title(): Set the title of the plot

    plt.plot(x, np.sin(x))
    plt.title('Sine Wave')
    plt.show()
    
  11. plt.xlabel() and plt.ylabel(): Set axis labels

    plt.plot(x, np.sin(x))
    plt.xlabel('x')
    plt.ylabel('sin(x)')
    plt.show()
    
  12. plt.legend(): Add a legend to the plot

    plt.plot(x, np.sin(x), label='sin')
    plt.plot(x, np.cos(x), label='cos')
    plt.legend()
    plt.show()
    
  13. plt.grid(): Add a grid to the plot

    plt.plot(x, np.sin(x))
    plt.grid(True)
    plt.show()
    
  14. plt.xlim() and plt.ylim(): Set the axis limits

    plt.plot(x, np.sin(x))
    plt.xlim(0, 5)
    plt.ylim(-1, 1)
    plt.show()
    
  15. plt.savefig(): Save the plot to a file

    plt.plot(x, np.sin(x))
    plt.savefig('sine_wave.png')
    
  16. plt.close(): Close the current figure

    plt.plot(x, np.sin(x))
    plt.show()
    plt.close()
    
  17. plt.colorbar(): Add a colorbar to the plot

    plt.imshow(np.random.rand(10, 10), cmap='viridis')
    plt.colorbar()
    plt.show()
    
  18. plt.axhline() and plt.axvline(): Add horizontal/vertical lines

    plt.plot(x, np.sin(x))
    plt.axhline(y=0, color='r', linestyle='--')
    plt.axvline(x=np.pi, color='g', linestyle='--')
    plt.show()
    
  19. plt.text(): Add text to the plot

    plt.plot(x, np.sin(x))
    plt.text(np.pi/2, 1, 'Maximum')
    plt.show()
    
  20. plt.annotate(): Add annotations to the plot

    plt.plot(x, np.sin(x))
    plt.annotate('Local maximum', xy=(np.pi/2, 1), xytext=(np.pi/2, 1.5),
                 arrowprops=dict(facecolor='black', shrink=0.05))
    plt.show()
    
  21. plt.pie(): Create a pie chart

    sizes = [30, 40, 20, 10]
    plt.pie(sizes, labels=['A', 'B', 'C', 'D'], autopct='%1.1f%%')
    plt.show()
    
  22. plt.polar(): Create a polar plot

    r = np.arange(0, 2, 0.01)
    theta = 2 * np.pi * r
    plt.polar(theta, r)
    plt.show()
    
  23. plt.errorbar(): Plot with error bars

    x = np.linspace(0, 4, 10)
    y = np.exp(-x)
    yerr = 0.1 + 0.2*np.sqrt(x)
    plt.errorbar(x, y, yerr=yerr, fmt='o')
    plt.show()
    
  24. plt.stem(): Create a stem plot

    x = np.linspace(0, 2*np.pi, 10)
    y = np.sin(x)
    plt.stem(x, y)
    plt.show()
    
  25. plt.step(): Create a step plot

    x = np.arange(10)
    y = np.random.randn(10)
    plt.step(x, y)
    plt.show()
    
  26. plt.violinplot(): Create a violin plot

    data = [np.random.normal(0, std, 100) for std in range(1, 5)]
    plt.violinplot(data)
    plt.show()
    
  27. plt.quiver(): Create a quiver plot

    x, y = np.meshgrid(np.arange(-2, 2, .2), np.arange(-2, 2, .2))
    u = x
    v = y
    plt.quiver(x, y, u, v)
    plt.show()
    
  28. plt.fill_between(): Fill area between two curves

    x = np.linspace(0, 10, 100)
    y1 = np.sin(x)
    y2 = np.sin(x) + 0.5
    plt.fill_between(x, y1, y2)
    plt.show()
    
  29. plt.rcParams: Modify default plot settings

    plt.rcParams['font.size'] = 14
    plt.plot(x, np.sin(x))
    plt.show()
    
  30. plt.tight_layout(): Adjust subplot parameters for optimal layout

    plt.subplot(211)
    plt.plot(x, np.sin(x))
    plt.subplot(212)
    plt.plot(x, np.cos(x))
    plt.tight_layout()
    plt.show()
    

Citations:

[1] https://machinelearningmastery.com/ a-gentle-introduction-to-scikit-learn-a-python-machine-learning-library/
[2] https://www.w3schools.com/python/
[3] https://en.wikipedia.org/wiki/ Python_%28programming_language%29
[4] https://www.linkedin.com/pulse/ introduction-scikit-learn-library-python-machine-learning-aritra-pain
[5] https://zerotomastery.io/blog/how-to-use-scikit-learn/