Module 5: PyBer with Matplotlib
Object-Oriented Interface Function fig, ax = plt.subplots(figsize=(w, h))
Change the size of the figure in pixels. Add this in the subplots() function.
plt.figure(figsize=(w, h))
Change the size of the figure in pixels. Added on the first line of the script.
plt.gca()
get current axes. example (plt.gca()invert_yaxis( )
plt.xlabel()
labels x axis of plot
plt.ylabel()
labels y axis of plot
plt.legend()
makes a legend for the plot
errorbar()
shows a plot with error bars at each data point x = 1:4; y = [5,8,13,9]; err = [1,2,4,1]; h = errorbar(x, y, err);
% (magic command)
The % sign is sometimes called the magic command. When you run a cell that starts with %, "magic" happens behind the scenes and sets the back-end processor used for charts.
plt.pie()
The function to create a pie chart
barh()
a horizontal bar graph
Matrix
a two-dimensional data structure, or a list of lists, in which numbers are arranged into rows and columns
plt.barh(x,y)
Add a Horizontal Bar Chart
plt.bar(x,y)
Add a Vertical Bar Chart
Object-Oriented Interface Function ax.grid()
Add a grid to the chart.
plt.grid()
Add a grid to the chart.
Object-Oriented Interface Function ax.plot(x, y, label='line')
Add a label that will be added to the legend.
plt.plot(x, y, label='line')
Add a label that will be added to the legend.
Object-Oriented Interface Function ax.set_xlabel('x label')
Add a label to the x-axis.
plt.xlabel('x label')
Add a label to the x-axis.
Object-Oriented Interface Function ax.set_ylabel('y label')
Add a label to the y-axis.
plt.ylabel('y label')
Add a label to the y-axis.
Object-Oriented Interface Function ax.legend()
Add a legend.
Object-Oriented Interface Function ax.set_title("Title")
Add a title.
plt.title("Title")
Add a title.
fig, ax = plt.subplots() ax.errorbar(x_axis, y_axis, yerr=stdev, capsize=3) plt.show()
Adding error bars and a capsize using the object-oriented approach
plt.bar(x_axis, y_axis, yerr=stdev, capsize=3)
Adding error bars and caps to a bar chart using the MATLAB approach
regression analysis
An analytic technique where a series of input variables are examined in relation to their corresponding output results in order to develop a mathematical or statistical relationship.
Object-Oriented Interface Function ax.set_ylim(min, max)
Sets the min and max range of the y-axis.
import matplotlib.pyplot as plt
In order to create a graph in Python, you need to include
The Numpy Module
NumPy is a numerical mathematics library that can be used to create arrays or matrices of numbers.
list comprehension
Python rules for creating lists intelligently s = [x for x in range(1:51) if x%2 == 0] [2, 4, 6, 8, 10, 12, 14, 16, etc]
plt.savefig("add a path and figure extension")
Save the figure with the given extension. Added at the end of the script.
Object-Oriented Interface Function ** plt.savefig("add a path and figure extension")
Saves the figure with the given extension. Added at the end of your script.
plt.xlim(min, max)
Set the min and max range of the x-axis.
plt.ylim(min, max)
Set the min and max range of the y-axis.
Object-Oriented Interface Function ax.set_xlim(min, max)
Sets the min and max range of the x-axis.
plt.plot()
The purpose of __________ is to create the figure with the current axes.
explode values
To "explode" a pie chart means to make one of the wedges of the pie chart to stand out.
ax.bar(x,y)
To create a bar chart using the object-oriented interface method
fig, ax = plt.subplots()
To create a figure, use __________ when using the object-orientated interface.
ax.barh(x,y)
To create a horizontal bar chart using the object-oriented interface method
plt.plot() Function to change color, width, and marker
To declare the parameters for line color, width, and marker, we use color=, width=, and marker=, respectively, inside the plt.plot() function.
stdev = statistics.stdev(y_axis) stdev
To get the standard deviation of the values in the y-axis
%matplotlib inline
To make sure our plots are displayed inline within the Jupyter Notebook directly below the code cell that produced it, we need to add ________________ to the first cell
Tuple
Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets. example: tuple = ("apple", "banana", "cherry")
ax.plot()
When we switch from using MATLAB to the object-oriented interface method, we use __________ instead of plt.plot().
plt.plot(x_axis, y_axis, 'o')
When we use the plt.plot() function to create a scatter plot, we need to add a lowercase "o" as a parameter inside the parentheses. This switches the plot from a line chart to a scatter plot.
plt.show()
displays the graph
plt.title()
to add a title to your chart
plt.errorbar(x_axis, y_axis, yerr=stdev)
to create a line chart with error bars