matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure. For example, creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.
In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of things like the current figure and plotting area. The plotting functions are directed to the current axes (please note that "axes" here and in most places in the documentation refers to the axes part of a figure and not the strict mathematical term for more than one axis).
It allows you to create and customize the most common types of charts, including:
When you want to work with matplotlib locally, you should run the following command:
pip install matplotlib
\
or\
conda install matplotlib
In our case, 4Geeks have prepared all the environment in order that you can work comfortably.
plt
" (★☆☆)¶Which one is "correct"? Check PEP8 (https://www.python.org/dev/peps/pep-0008/#imports)
To create a graph with matplotlib it is usual to follow the following steps:
An "empty" plot will be created if you run the following lines:
# empty plot
fig, ax = plt.subplots()
x = [1, 2, 3, 4], y = [1, 2, 0, 0.5]
Use ax.scatter
x = [1, 2, 3, 4], y = [1, 2, 0, 0.5]
Use ax.plot
x = [1, 2, 3, 4], y = [1, 2, 0, 0.5]
Use ax.fill_between
Note: Remember np.random
and use ax.hist.
The next image is a boxplot. A boxplot is a standardized way of displaying the distribution of data based on a five number summary (“minimum”, first quartile (Q1), median, third quartile (Q3), and “maximum”). It can tell you about your outliers and what their values are. It can also tell you if your data is symmetrical, how tightly it is grouped, and if and how it is skewed.
Boxplots have the following characteristics:
What defines an outlier, “minimum” or “maximum” may not be clear yet. We will explain it in the bootcamp.
Use ax.boxplot
Use cv2.imread
\
Use ax.imshow
\
Do you see the image exactly as it is? What do you think happened?
The graphics created with matplotlib are customizable and the appearance of almost all its elements can be changed. The elements that are most often modified are:
To change the color of the objects, use the parameter color = color-name, where color-name is a string with the name of the color from among the available colors.
Check the full list here: https://matplotlib.org/stable/gallery/color/named_colors.html
It is possible to draw several graphs in different axes in the same figure organized in table form. To do this, when the figure and the axes are initialized, the number of rows and columns of the table that will contain the graphs must be passed to the subplots function. With this, the different axes are organized in an array and each of them can be accessed through their indexes. If you want the different axes to share the same limits for the axes, you can pass the parameters sharex = True
for the x axis or sharey = True
for the y axis.
## Input
s = pd.Series({'Math': 6.0, 'Economy': 4.5, 'Programming': 8.5})
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2, sharey = True)
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
temperature = {'Madrid':[28.5, 30.5, 31, 30, 28, 27.5, 30.5], 'Barcelona':[24.5, 25.5, 26.5, 25, 26.5, 24.5, 25]}
ax[0, 0].plot(days, temperature['Madrid'])
ax[0, 1].plot(days, temperature['Barcelona'], color = 'tab:orange')
ax[1, 0].bar(days, temperature['Madrid'])
ax[1, 1].bar(days, temperature['Barcelona'], color = 'tab:orange')
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'Days':['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
'Madrid':[28.5, 30.5, 31, 30, 28, 27.5, 30.5],
'Barcelona':[24.5, 25.5, 26.5, 25, 26.5, 24.5, 25]})
fig, ax = plt.subplots()
df.plot(x = 'Days', y = 'Madrid', ax = ax)
df.plot(x = 'Days', y = 'Barcelona', ax = ax)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
pop = pd.read_csv("pop_hist.csv")
pop = pop.iloc[np.where(pop.AGE=="TOTAL") and np.where(pop.LOCATION!="OECD") and (np.where(pop.LOCATION=="ESP") or np.where(pop.LOCATION=="USA")) ]
pop = pop.loc[:,["LOCATION","TIME","Value"]]
pop["Value"] = pop["Value"].astype(int)
fig, ax = plt.subplots()
pop.plot(x = 'TIME', y = 'Value', ax = ax)
pop.plot(x = 'TIME', y = 'Value', ax = ax)
plt.show()
3-D Dimensional data provides the perception of depth, width, and height (it can be viewed from any angle). Three-dimensional visualizations were developed to provide both qualitative and quantitative information about an object. 3D visualizations are visualized with the three-phase process of scene, geometry, and rendering. Datasets increase in size, the need for analysis and visualization tools for the data also becomes essential.
Analysis operations, like visualization operations, may be either scene-based or object-based and deal with methods of quantifying object information.
Some examples of 3D shapes are prisms, pyramids, spheres, cones, cubes, or even figures!!!! 😲😲.
Three-Dimensional visualizations represent visualizations in all angles with just turning off the camera in the scene. While considering the two-dimensional formats, there is a limit on how much information to take and use visualization for making decisions, planning and targeting customers. Three-Dimensional visualization allows to draw which character of the scene changed. It easily communicates with the internal features. Some of the applications include GIS(Geographic Information Systems), geographic visualizations in a three-dimensional view, provides more interaction which is essential for understanding. It gives a sense of immersion of the environment where the user appreciates the scale of change and visualizes the impact of building design on the external environment and the inhabitants. GIS examples will include city planning, build information planning, coastal analysis, and modeling and wind farm assessment.
# We enable three-dimensional plots by importing the mplot3d toolkit
from mpl_toolkits import mplot3d
# Once this submodule is imported, we can create a three-dimensional axes by passing the keyword projection='3d' to any of the normal axes creation routines
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')
Check the function
rename
: (https://matplotlib.pydata.org/docs/reference/api/matplotlib.DataFrame.rename.html)
#Three-Dimensional Contour Plots
def f(x,y):
return np.sin(np.sqrt(x**2+y**2))
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X,Y=np.meshgrid(x,y)
Z=f(X,Y)
fig=plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
#Sometimes the default viewing angle is not optimal, in which case we can use the view_init method to set the elevation and azimuthal angles.
ax.view_init(60,35)
fig