Programming Tutorials

Home OF Basic Programmers

Matplotlib | Pie Charts

Creating Pie Charts With Pyplot, you can use the pie() function to draw pie charts: Example A simple pie chart: import matplotlib.pyplot as pltimport numpy as np y = np.array([35, 25, 25, 15]) plt.pie(y)plt.show()  Result: As you can see the pie chart draws one piece (called a wedge) for each value in the array (in this case [35, 25, 25, 15]). By default the plotting of …

Matplotlib | Pie Charts Read More »

Matplotlib | Histograms

Histogram A histogram is a graph showing frequency distributions. It is a graph showing the number of observations within each given interval. Example: Say you ask for the height of 250 people, you might end up with a histogram like this: You can read from the histogram that there are approximately: 2 people from 140 to 145cm5 …

Matplotlib | Histograms Read More »

Matplotlib | Bars

Creating Bars With Pyplot, you can use the bar() function to draw bar graphs: Example Draw 4 bars: Result: The bar() function takes arguments that describes the layout of the bars. The categories and their values represented by the first and second argument as arrays. Example x = [“APPLES”, “BANANAS”]y = [400, 350]plt.bar(x, y) Horizontal Bars If you want the bars to be displayed horizontally …

Matplotlib | Bars Read More »

Matplotlib | Scatter

Creating Scatter Plots With Pyplot, you can use the scatter() function to draw a scatter plot. The scatter() function plots one dot for each observation. It needs two arrays of the same length, one for the values of the x-axis, and one for values on the y-axis: Example A simple scatter plot: import matplotlib.pyplot as pltimport numpy as np x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86]) plt.scatter(x, …

Matplotlib | Scatter Read More »

Matplotlib | Subplot

Display Multiple Plots With the subplot() function you can draw multiple plots in one figure: Example Draw 2 plots: import matplotlib.pyplot as pltimport numpy as np #plot 1:x = np.array([0, 1, 2, 3])y = np.array([3, 8, 1, 10]) plt.subplot(1, 2, 1)plt.plot(x,y) #plot 2:x = np.array([0, 1, 2, 3])y = np.array([10, 20, 30, 40]) plt.subplot(1, 2, 2)plt.plot(x,y) plt.show() Result: The subplot() Function The subplot() function takes three arguments that describes the layout of the figure. The layout is organized in rows and columns, …

Matplotlib | Subplot Read More »

Matplotlib | Adding Grid Lines

Add Grid Lines to a Plot With Pyplot, you can use the grid() function to add grid lines to the plot. Example Add grid lines to the plot: import numpy as npimport matplotlib.pyplot as plt x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330]) plt.title(“Sports Watch Data”)plt.xlabel(“Average Pulse”)plt.ylabel(“Calorie Burnage”) plt.plot(x, y) plt.grid() plt.show() Result: Specify Which Grid Lines to Display You can use the axis parameter in the grid() function to specify …

Matplotlib | Adding Grid Lines Read More »

Matplotlib | Labels and Title

Create Labels for a Plot With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis. Example Add labels to the x- and y-axis: import numpy as npimport matplotlib.pyplot as plt x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330]) plt.plot(x, y) plt.xlabel(“Average Pulse”)plt.ylabel(“Calorie Burnage”) plt.show() Result: Create a Title for a Plot With Pyplot, you can use the title() function to set …

Matplotlib | Labels and Title Read More »

Matplotlib | Line

Linestyle You can use the keyword argument linestyle, or shorter ls, to change the style of the plotted line: Example Use a dotted line: Result: Example Use a dashed line: plt.plot(ypoints, linestyle = ‘dashed’) Result: Shorter Syntax The line style can be written in a shorter syntax: linestyle can be written as ls. dotted can be written as :. dashed can be written …

Matplotlib | Line Read More »

Matplotlib | Markers

Markers You can use the keyword argument marker to emphasize each point with a specified marker: Example Mark each point with a circle: import matplotlib.pyplot as pltimport numpy as np ypoints = np.array([3, 8, 1, 10]) plt.plot(ypoints, marker = ‘o’)plt.show() Result: Example Mark each point with a star: …plt.plot(ypoints, marker = ‘*’)… Result: Marker Reference You can choose any of these markers: Marker Description ‘o’ Circle ‘*’ Star …

Matplotlib | Markers Read More »

Matplotlib | Plotting

Plotting x and y points The plot() function is used to draw points (markers) in a diagram. By default, the plot() function draws a line from point to point. The function takes parameters for specifying points in the diagram. Parameter 1 is an array containing the points on the x-axis. Parameter 2 is an array containing the points on the y-axis. …

Matplotlib | Plotting Read More »