Matplotlib is a Python library used for creating graphs. The `pyplot` module in Matplotlib provides functions to make various types of plots. You need to import this module, and `plt` is the common alias used for it.
from matplotlib import pyplot as plt # Now you can use plt to create plots
Pyplot includes many functions to create different kinds of plots. Here are some basics to get you started:
from matplotlib import pyplot as plt # Example of a basic plot plt.plot([1, 2, 3], [4, 5, 6]) plt.show() # This code plots a simple line graph
You can customize the appearance of your plot using `plt.axis()`. This function lets you set the limits for the x-axis and y-axis by providing an array of four values: [x_min, x_max, y_min, y_max].
x = range(12) y = [2, 8, 20, 40, 70, 300, 930, 7000, 68000, 500000, 4000000, 2000000] plt.plot(x, y) plt.axis([0, 11, 300, 500000]) plt.show() # This sets the limits for the x and y axes and plots the data
In Matplotlib, you can change the color, style, and markers of your plot lines using parameters in `plt.plot()`. For example, you can set line color, style (solid, dashed, dotted), and markers (circles, squares).
from matplotlib import pyplot as plt days = [1, 2, 3, 4] money_spent = [10, 20, 30, 40] plt.plot(days, money_spent, color='green', linestyle='--', marker='o') plt.show() # This plots a line with green dashed style and circular markers
When creating multiple plots in one figure, you might need to adjust the spacing between them. Use `plt.subplots_adjust()` to set margins and space between subplots. Parameters include `left`, `right`, `bottom`, `top`, `wspace`, and `hspace`.
import matplotlib.pyplot as plt plt.subplot(1, 2, 1) plt.plot([1, 2, 3], [4, 5, 6]) plt.subplot(1, 2, 2) plt.plot([1, 2, 3], [6, 5, 4]) plt.subplots_adjust(wspace=0.5) plt.show() # Adjusts space between two plots side by side
To customize the tick marks on the x-axis and y-axis, use `ax.set_xticks()` and `ax.set_yticks()`. These functions let you specify where the ticks should appear.
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [1, 4, 9]) ax.set_xticks([1, 2, 3]) plt.show() # Sets specific positions for x-axis ticks
To create a figure with multiple subplots, use `plt.subplot()`. You need to specify the number of rows and columns and the index of the current subplot.
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [1, 2, 3, 4] plt.subplot(1, 2, 1) plt.plot(x, y, color='green') plt.title('First Plot') plt.subplot(1, 2, 2) plt.plot(x, y, color='blue') plt.title('Second Plot') plt.show() # Creates a figure with two side-by-side plots
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!