|
| 1 | +--- |
| 2 | +Title: '.plot()' |
| 3 | +Description: 'Creates line and marker plots to visualize relationships between variables in Matplotlib.' |
| 4 | +Subjects: |
| 5 | + - 'Data Science' |
| 6 | + - 'Data Visualization' |
| 7 | +Tags: |
| 8 | + - 'Charts' |
| 9 | + - 'Matplotlib' |
| 10 | + - 'Methods' |
| 11 | + - 'Python' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | + - 'paths/data-science-foundations' |
| 16 | +--- |
| 17 | + |
| 18 | +The **`.plot()`** function in [Matplotlib](https://www.codecademy.com/resources/docs/matplotlib/pyplot) is the primary method used to create line plots and marker plots. It takes one or two sets of data points (x and y coordinates) and draws lines connecting them, displays markers at each point, or both. This function is one of the most commonly used tools in data visualization for showing trends, patterns, and relationships between variables. |
| 19 | + |
| 20 | +Line plots are ideal for illustrating how data changes over time or across continuous intervals. It's widely used in fields such as statistics, engineering, and data science for exploratory data analysis and model visualization. |
| 21 | + |
| 22 | +## Syntax |
| 23 | + |
| 24 | +```pseudo |
| 25 | +matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs) |
| 26 | +``` |
| 27 | + |
| 28 | +**Parameters:** |
| 29 | + |
| 30 | +- `*args`: Values for y, or x and y, optionally followed by a format string. |
| 31 | +- `scalex`: Auto-scale the x-axis (default True). |
| 32 | +- `scaley`: Auto-scale the y-axis (default True). |
| 33 | +- `data`: Optional dict or DataFrame for referencing variables by name. |
| 34 | +- `**kwargs`: Style and configuration options like color, label, linewidth, marker. |
| 35 | + |
| 36 | +**Return value:** |
| 37 | + |
| 38 | +Returns a list of `Line2D` objects representing the plotted data. |
| 39 | + |
| 40 | +## Example: Creating a Basic Line Plot |
| 41 | + |
| 42 | +This example demonstrates how to create a simple line plot with plt.plot() to visualize the relationship between two numerical variables: |
| 43 | + |
| 44 | +```py |
| 45 | +import matplotlib.pyplot as plt |
| 46 | +import numpy as np |
| 47 | + |
| 48 | +# Sample data |
| 49 | +x = np.array([1, 2, 3, 4, 5]) |
| 50 | +y = np.array([2, 4, 1, 6, 3]) |
| 51 | + |
| 52 | +# Create a line plot |
| 53 | +plt.plot(x, y) |
| 54 | + |
| 55 | +# Add labels and a title |
| 56 | +plt.xlabel('X-axis') |
| 57 | +plt.ylabel('Y-axis') |
| 58 | +plt.title('Basic Line Plot Example') |
| 59 | + |
| 60 | +# Add a grid for better readability |
| 61 | +plt.grid(True, linestyle='--', alpha=0.6) |
| 62 | + |
| 63 | +# Display the plot |
| 64 | +plt.show() |
| 65 | +``` |
| 66 | + |
| 67 | +This example visualizes the relationship between an independent variable, X-axis, and a dependent variable, Y-axis, using a single continuous line. The plot shows the change in Y values as X increases from 1.0 to 5.0. This plot highlights a few useful details worth noting: |
| 68 | + |
| 69 | +- Single Series: The plot displays one series of data, represented by a single blue line, showing the progression of the Y-axis value relative to the X-axis value. |
| 70 | +- Data Points and Trend: The line connects the following data points: $(1.0, 2.0)$, $(2.0, 4.0)$, $(3.0, 1.0)$, $(4.0, 6.0)$, and $(5.0, 3.0)$. The plot illustrates clear changes in direction: an initial increase, a sharp decrease, a significant increase to the maximum value, and a final decrease. |
| 71 | +- Labels and Title: The plot is clearly titled "Basic Line Plot Example" and has "X-axis" and "Y-axis" labels for clarity. |
| 72 | +- Grid: A light dashed grid is present, running horizontally and vertically across the plot, which aids in precisely reading the coordinate values of the data points off the chart. |
| 73 | + |
| 74 | + |
0 commit comments