Install
The easiest way to install matplotlib — this is to use pip. Enter the following command in terminal:
pip install matplotlib
OR you can download it from here and install manually.
Getting started (laying the line)
|
Output:
The code seems self-evident. The following steps were taken:
- Define the X-axis and corresponding Y-axis values as lists.
- Place them on the canvas using the .plot () function .
- Name the X and Y axes using the .xlabel () and .ylabel () functions.
- Name your graphic, using the .title () function .
- Finally, we use .show () function .
Draw two or more lines on the same site
|
Output:
- Here we are plotting two lines on one graph. We distinguish them by giving them a name ( label ), which is passed as an argument to the .plot () function.
- The small rectangle-method/">rectangle with information about the line type and its color is called a legend. We can add a legend to our graphics using the .legend () function .
C ustomization from parcels
Here we discuss some basic settings that apply to almost any plot.
|
Output:
As you can see, we have made several settings such as
- setting line width, line style, line color.
- mar setting ker, marker face color, marker size.
- override the X and Y axis range. If not overridden, the pyplot module uses autoscale to set the range and scale of the axis.
Histogram
|
Output:
- Here we use the plt.bar () function to plot the bar graph.
- The X-coordinates of the left side of the bars are passed along with the height of the bars.
- You can also name the X-axis coordinates by defining tick_labels
Histogram
|
Output:
- Here we use the plt.hist () function to plot the histogram.
- frequencies are transmitted as a list of ages .
- The range can be set by specifying a tuple containing the minimum and maximum values.
- The next step is “ selection "range of values, that is, section Dividing the entire range of values into a number of bins, and then counting the number of values that fall within each bin. Here we have defined bins = 10. So there are 100/10 = 10 bins.
Dot plot
|
|
The output of the above program looks like this:
- Here we are build a pie chart using the plt.pie () method .
- First of all, we define labels using a list called actions .
- Then a portion of each label can be defined using another list called slices .
- The color for each label is defined with using a list called colors .
- shadow = True will show the shadow by e each label on the pie chart.
- startangle rotates the start of the pie chart the specified degrees counterclockwise from the x-axis.
- explode is used to set the fraction of the radius with which we offset each wedge.
- autopct is used to format the value of each label. Here we have set it to only show a percentage up to 1 decimal place.
Plot the curves of this equation
| tr>
The output of the above program looks like this:
Here we use NumPy, which is a generic package for handling arrays in Python.
- To set the x-axis values, we we use the np.arange () method, in which the first two arguments are for a range, and the third is — for incremental increments. The result is a NumPy array.
- To get the corresponding Y-axis values, we simply use the predefined np.sin () method on the numpy array.
- Finally, we plot the points by passing the x and y arrays to the plt.plot () function .
So, in this part, we discussed the different types of plots that we can create in matplotlib. There are a few more plots that were not covered, but the most significant ones are discussed here —
- Plotting in Python | Set 2
-
Shop
Latest questions
Wiki