PLOTTING BASIC FIGURES IN MATPLOTLIB

 PLOTTING BASIC FIGURES IN MATPLOTLIB WITH EXAMPLE

Line Plot components in Matplotlib

  • A line plot in Matplotlib consists of several components that you can customize to create a visually appealing and informative graph. 
  • Here are the key components of a line plot in Matplotlib:

 1. Figure and Axes:

   - Figure (`plt.figure()`): The top-level container that holds the entire plot. You can customize the figure size, background color, and other properties.

    - Axes (`plt.subplot()` or `plt.subplots()`): The area within the figure where the data is plotted. You can have multiple axes in a single figure.

 2. Data Plotting:

   - Plotting Function (`plt.plot()`): This is where you specify the data to be plotted. The most common use is to connect points with lines, but you can also use markers without lines or choose other plot types.

     plt.plot(x, y, label='sin(x)', color='blue', linestyle='-', linewidth=2, marker='o', markersize=5)

     - `x` and `y`: The data to be plotted along the x and y axes.

 3. Title and Labels:

   - Title (`plt.title()`): Adds a title to the plot.

     plt.title('Line Plot of sin(x)')

    - Axis Labels (`plt.xlabel()` and `plt.ylabel()`): Adds labels to the x and y axes.

     plt.xlabel('x')

   plt.ylabel('sin(x)')

  4. Legend:

   - Legend (`plt.legend()`): Displays a legend that describes the elements in the plot. The `label` parameter in the plotting function is used to associate a label with each data series.

     plt.legend()

  5. Grid Lines:

   - Grid (`plt.grid()`): Adds grid lines to the plot.

     plt.grid(True)

 6. Customizing Line Appearance:

   - Color (`color`): Specifies the color of the line.

   - Linestyle (`linestyle`): Determines the style of the line (solid, dashed, dotted, etc.).

   - Linewidth (`linewidth`): Sets the width of the line.

   - Marker (`marker`): Specifies the marker style for data points.

   - Markersize (`markersize`): Sets the size of markers.

    These properties are specified in the `plt.plot()` function.

 7. Saving and Displaying the Plot:

   - Save (`plt.savefig()`): Saves the plot to a file.

   - Show (`plt.show()`): Displays the plot on the screen.

     plt.show()

  example:

import matplotlib.pyplot as plt

import numpy as np

 x = np.linspace(0, 2 * np.pi, 100)

y = np.sin(x)

 plt.figure(figsize=(8, 4))

plt.plot(x, y, label='sin(x)', color='blue', linestyle='-', linewidth=2, marker='o', markersize=5)

plt.title('Line Plot of sin(x)')

plt.xlabel('x')

plt.ylabel('sin(x)')

plt.grid(True)

plt.legend()

plt.show()





Post a Comment

0 Comments