0% found this document useful (0 votes)
25 views

Data Visualization Lab3

1. Filter the dataset to samples containing body mass and maximum longevity 2. Select Aves samples with body mass < 20,000 3. Create a Figure with a 4x4 GridSpec for constrained layout 4. Add a 3x3 scatter plot and 1x3 & 3x1 marginal histograms, with labels and a title

Uploaded by

yomna mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Data Visualization Lab3

1. Filter the dataset to samples containing body mass and maximum longevity 2. Select Aves samples with body mass < 20,000 3. Create a Figure with a 4x4 GridSpec for constrained layout 4. Add a 3x3 scatter plot and 1x3 & 3x1 marginal histograms, with labels and a title

Uploaded by

yomna mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Lab 3

Matplotlib
More Plots

1
Histogram
• When to use Histogram
– The histogram is used to showcase a graphical presentation that represents the
numerical data in the form of frequency
• Create Histogram
– plt.hist(x)
– x: Specifies the input values
– bins: (optional): Either specifies the number of bins as an integer or specifies the bin
edges as a list
– range: (optional): Specifies the lower and upper range of the bins as a tuple
– density: (optional): If true, the histogram represents a probability density

2
Histogram
• Example
– plt.hist(x, bins=30, density=True)

3
Box Plot
• When to use Box Plot
– Help visualize the distribution of quantitative values in a field. They are
also valuable for comparisons across different categorical variables
– Horizontal line indicates median, the whiskers extending from the box
show the range of the data or identifying outlies.

Box Plot for single


variable
Q3

Q1

4
Box Plot
• A box plot is constructed from five values:
– Minimum value
– First quartile
– Median
– Third quartile
– Maximum value
• Create Box Plot
– plt.boxplot(x)
– x: Specifies the input data. It specifies either a 1D array for a single box or a
sequence of arrays for multiple boxes.
– notch: Optional: If true, notches will be added to the plot to indicate the confidence
interval around the median.
– labels: Optional: Specifies the labels as a sequence.
– showfliers: Optional: By default, it is true, and outliers are plotted beyond the caps.
– showmeans: Optional: If true, arithmetic means are shown.

5
Box Plot
• Create Box Plot
– plt.boxplot([x1, x2], labels=['A', 'B'])

6
Ex 1:Visualize Intelligence Quotient
Using Histogram with mean 100 and standard deviation 15

7
Ex 1:Visualize Intelligence Quotient
• Using Box Plot

8
Ex 1:Visualize Intelligence Quotient
• Using Box Plot and different test groups

9
Scatter Plot
• When to use Scatter plot
– It is used to observe and show relationships (correlation) between two numeric
variables.
– They allow you to plot the relationship for multiple groups or categories using
different colors.

10
Scatter Plot
• To create Scatter plot
– plt.scatter(x, y)
• x, y: Specifies the data positions.
• s: Optional: Specifies the marker size in points squared.
• c: Optional: Specifies the marker color. If a sequence of numbers is
specified, the numbers will be mapped to colors of the color
map.

11
Scatter Plot
• Types of correlation

12
Bubble Plot
• It is extending scatter plot by introducing 3rd numerical variable.
• When to use bubble plot:
– To show correlation between 3 variables.

13
Bubble Plot
• To create bubble plot
– To visualize a third or a fourth variable, the parameters s (scale) and c (color)
can be used.
– plt.scatter(x, y, s=z*500, c=c, alpha=0.5)
– plt.colorbar()
– Scale may be scalar or array which its len = to x and y

14
Ex 2:Visualize correlation bet.
Various animals
• Axes.set_xscale('log') and Axes.set_yscale('log') change the scale of the x-axis and
y-axis to a logarithmic scale
• The given dataset is not complete. Filter the data so that you end up with samples
containing a body mass and a maximum longevity.
• Sort the data according to the animal class.

15

Subplot
• We will start with subplots and how to use the tight layout to create
visually appealing plots
• Also,GridSpec will be covered, which offers a more flexible way to create
multi-plots.
– plt.subplots(nrows, ncols): creates a Figure and a set of subplots.
– plt.subplot(nrows, ncols, index) or plt.subplot(pos): adds a subplot to the current Figure, index
starts at 1.
• Ex:plt.subplot(2, 2, 1) is equivalent to plt.subplot(221).
– Fig.add_subplot(nrows,ncols,index) or Fig.add_subplot(pos) adds a subplot to the specified
Figure.

16
Subplot
• Example
– fig, axes = plt.subplots(2, 2)
– axes = axes.ravel() Note: in 2nd part we can replace
2nd line by:
– for i, ax in enumerate(axes): Fig=plt.figure()
– ax.plot(series[i]) Fig.add_subplot(2, 2, i+1)

• OR
– for i in range(4):
– plt.subplot(2, 2, i+1)
– plt.plot(series[i])

17
Subplot
• For sharing the x or y axis, the parameters sharex and sharey
must be set, respectively. The axis will have the same limits, ticks,
• and scale.
– fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
– axes = axes.ravel()
– for i, ax in enumerate(axes):
– ax.plot(series[i])

18
Tight Layout
• plt.tight_layout() adjusts subplot parameters so that the subplots fit well
in the Figure.
• If you don’t use it subplots might overlap.

fig, axes = plt.subplots(2, 2)


axes = axes.ravel()
for i, ax in enumerate(axes):
ax.plot(series[i])
ax.set_title('Subplot ' + str(i))

19
Tight Layout
• plt.tight_layout() adjusts subplot parameters so that the subplots fit well
in the Figure.
• If you don’t use it subplots might overlap.

fig, axes = plt.subplots(2, 2)


axes = axes.ravel()
for i, ax in enumerate(axes):
ax.plot(series[i])
ax.set_title('Subplot ' + str(i))
plt.tight_layout()

20
GridSpec
• GridSpec provides us with additional control over the placements of subplots,
also the the margins and the spacings between the individual subplots.
– matplotlib.gridspec.GridSpec(nrows, ncols)#specifies the geometry of the grid
in which a subplot will be placed.

• gs = matplotlib.gridspec.GridSpec(3, 4)
• ax1 = plt.subplot(gs[:3, :3])
• ax2 = plt.subplot(gs[0, 3])
• ax3 = plt.subplot(gs[1, 3])
• ax4 = plt.subplot(gs[2, 3])
• ax1.plot(series[0])
• ax2.plot(series[1])
• ax3.plot(series[2])
• ax4.plot(series[3])
• plt.tight_layout()

21
Ex3: Creating Scatter Plot with marginal
Histogram

22
Ex3: Creating Scatter Plot with marginal
Histogram
1. The given dataset, AnAge,is not complete. Filter the data so that you end up
with samples containing a body mass and a maximum longevity.

2. Select all of the samples of the Aves class with body mass less than 20,000.

3. Create a Figure with a constrained layout . Create a gridspec of size 4x4.

4. Create a scatter plot of size 3x3 and marginal histograms of size 1x3 and
3x1. Add labels and a Figure title.

23

You might also like