📜  Matplotlib-Pyplot API(1)

📅  最后修改于: 2023-12-03 14:44:12.879000             🧑  作者: Mango

Matplotlib-Pyplot API

Matplotlib is a data visualization library in Python. Pyplot is a subpackage in matplotlib that provides a convenient interface for creating plots and charts. The pyplot API provides a way to create and customize plots with minimal code.

Installation

You can install Matplotlib using pip package manager. Open your terminal and type the following command:

pip install matplotlib
Creating a Plot

Pyplot provides a convenient API to create a plot with a single line of code. Here is an example:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 30, 40]

plt.plot(x, y)
plt.show()

The plot function creates a line plot with x as the x-axis and y as the y-axis. The show function displays the plot.

Customizing a Plot

You can customize various aspects of a plot using pyplot. Here are a few examples:

Adding a title and labels
plt.plot(x, y)
plt.title("My Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Changing the line style and color
plt.plot(x, y, linestyle="dashed", color="red")
plt.show()
Adding markers to the line plot
plt.plot(x, y, marker="o")
plt.show()
Adding multiple lines to the same plot
y2 = [15, 25, 35, 45]

plt.plot(x, y)
plt.plot(x, y2)
plt.show()
Conclusion

In this tutorial, we learned about the Matplotlib-Pyplot API, which provides a convenient way to create and customize plots in Python. We covered how to install Matplotlib, how to create a basic line plot, and how to customize various aspects of the plot using pyplot. With pyplot, you can create impressive plots with minimal effort.