0% found this document useful (0 votes)
5 views25 pages

Matplotlibv 3

Matplotlib is a versatile Python library for creating visualizations like charts and graphs, offering flexibility and control for data analysis. The document provides a comprehensive guide on how to install Matplotlib, import it, and execute various plotting exercises, including customizing markers, line styles, and adding titles and labels. It also includes examples of using grid lines and subplots for enhanced data presentation.

Uploaded by

f.tahraoui
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

Matplotlibv 3

Matplotlib is a versatile Python library for creating visualizations like charts and graphs, offering flexibility and control for data analysis. The document provides a comprehensive guide on how to install Matplotlib, import it, and execute various plotting exercises, including customizing markers, line styles, and adding titles and labels. It also includes examples of using grid lines and subplots for enhanced data presentation.

Uploaded by

f.tahraoui
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Matplotlib :

Matplotlib is a low-level graph plotting library in Python, primarily used to create visualizations
such as charts, graphs, and plots. Created by John D. Hunter, it is open-source and free to use.
While most of Matplotlib is written in Python, some parts are also written in C, Objective-C,
and JavaScript for better platform compatibility. It provides flexibility and control over
visualizations, making it a powerful tool for data analysis and presentation.

Matplotlib Lab: Getting Started

1. Install Matplotlib: Open the command prompt and run:

pip install matplotlib

2. Import Matplotlib: In your Python script, add:

import matplotlib

3. Check Matplotlib Version: To check the version, run:

import matplotlib

print(matplotlib.__version__)

Exercise 1:

1. Import the necessary libraries for plotting.


2. Create two arrays using NumPy:
o xpoints: A range of points from 0 to 6.
o ypoints: A range of points from 0 to 250.
3. Plot a line connecting the points (0, 0) to (6, 250).
4. Display the plot using [Link]().

Solution:

import [Link] as plt


import numpy as np

# Data points
xpoints = [Link]([0, 6])
ypoints = [Link]([0, 250])

# Plot the line


[Link](xpoints, ypoints)

# Show the plot


[Link]()

 import [Link] as plt: Imports the Pyplot module and assigns it the alias
plt for easier use.
 import numpy as np: Imports NumPy for creating arrays of data points.
 [Link](): Plots the data points as a line.
 [Link](): Displays the plot.
Exercise 2:

Draw a line from position (1, 3) to position (8, 10).

Solution:

import [Link] as plt


import numpy as np

# Data points
xpoints = [Link]([1, 8])
ypoints = [Link]([3, 10])

# Plot the line


[Link](xpoints, ypoints)

# Show the plot


[Link]()

Exercise 3:

Draw two points in the diagram, one at position (1, 3) and one at position (8, 10), using
markers only.

Solution:

import [Link] as plt


import numpy as np

# Data points
xpoints = [Link]([1, 8])
ypoints = [Link]([3, 10])

# Plot the points without a line


[Link](xpoints, ypoints, 'o')

# Show the plot


[Link]()

Exercise 4:

Draw a line connecting the points (1, 3), (2, 8), (6, 1), and (8, 10).

Solution:

import [Link] as plt


import numpy as np

# Data points
xpoints = [Link]([1, 2, 6, 8])
ypoints = [Link]([3, 8, 1, 10])

# Plot the line


[Link](xpoints, ypoints)

# Show the plot


[Link]()

Exercise 5:

Plot the points [3, 8, 1, 10, 5, 7] and use the default x-points (0, 1, 2, 3, 4, 5).

Solution:

import [Link] as plt


import numpy as np

# Data points (y-values)


ypoints = [Link]([3, 8, 1, 10, 5, 7])

# Plot the points with default x-values


[Link](ypoints)

# Show the plot


[Link]()

Exercise 6:

Create a plot where the points are marked with circles.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with circle markers


[Link](ypoints, marker='o')

# Show the plot


[Link]()

Exercise 7:

Use format string 'o:r' to specify the circle marker and a red dotted line.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with circle markers and red dotted line


[Link](ypoints, 'o:r')

# Show the plot


[Link]()

Exercise 8:

Increase the size of the markers in the plot to 20.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with marker size set to 20


[Link](ypoints, marker='o', ms=20)

# Show the plot


[Link]()

Exercise 9:

Change the marker's edge color (mec) and face color (mfc) to red.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with red edge and red face color


[Link](ypoints, marker='o', ms=20, mec='r', mfc='r')

# Show the plot


[Link]()

Marker Reference:

Marker Description
'o' Circle
'*' Star
'.' Point
',' Pixel
'x' X
'X' Filled X
'+' Plus
'P' Filled Plus
's' Square
'D' Diamond
'd' Thin Diamond
'p' Pentagon
'H' Hexagon
'h' Hexagon
'v' Triangle Down
'^' Triangle Up
'<' Triangle Left
'>' Triangle Right
'1' Tri Down
'2' Tri Up
'3' Tri Left
'4' Tri Right
' '
'_' Horizontal Line

Line Reference:

Line Syntax Description


'-' Solid line
':' Dotted line
'--' Dashed line
'-.' Dash-dot line

Color Reference:

Color Syntax Description


'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White

Exercise 10:

Plot a line using a dotted style.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with dotted line style


[Link](ypoints, linestyle=':')
# Show the plot
[Link]()

Exercise 11:

Plot a line using a dashed line style.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with dashed line style


[Link](ypoints, linestyle='--')

# Show the plot


[Link]()

Exercise 12:

Change the line style to a dotted line using the shorter syntax.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with dotted line using shorter syntax


[Link](ypoints, ls=':')

# Show the plot


[Link]()

Exercise 13:

Change the line color to red.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with red line color


[Link](ypoints, color='r')

# Show the plot


[Link]()
Exercise 14:

Change the line color to a green shade using a hexadecimal value.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with green color using hexadecimal value


[Link](ypoints, c='#4CAF50')

# Show the plot


[Link]()

Exercise 15:

Change the line width to 20.5 points.

Solution:

import [Link] as plt


import numpy as np

# Data points
ypoints = [Link]([3, 8, 1, 10])

# Plot with line width set to 20.5


[Link](ypoints, linewidth=20.5)

# Show the plot


[Link]()

Exercise 16:

Plot two different lines by using multiple [Link]() functions.

Solution:

import [Link] as plt


import numpy as np

# Data points for two lines


y1 = [Link]([3, 8, 1, 10])
y2 = [Link]([6, 2, 7, 11])

# Plot two lines


[Link](y1)
[Link](y2)

# Show the plot


[Link]()
Exercise 17:

Specify both x and y values for the two lines.

Solution:

import [Link] as plt


import numpy as np

# Data points for two lines


x1 = [Link]([0, 1, 2, 3])
y1 = [Link]([3, 8, 1, 10])
x2 = [Link]([0, 1, 2, 3])
y2 = [Link]([6, 2, 7, 11])

# Plot two lines with x and y values


[Link](x1, y1)
[Link](x2, y2)

# Show the plot


[Link]()

Line Style Reference:

Style Or Syntax
'solid' '-'
'dotted' ':'
'dashed' '--'
'dashdot' '-.'
'None' '' or ' '

Color Reference:

Color Syntax Description


'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White

Line Width Reference:

Argument Description
linewidth Width of the line in points (float)
Exercise 18:

Label the x-axis as "Average Pulse" and the y-axis as "Calorie Burnage".

Solution:

import numpy as np
import [Link] as plt

# Data points
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

# Plot the data


[Link](x, y)

# Add labels to the x- and y-axes


[Link]("Average Pulse")
[Link]("Calorie Burnage")

# Show the plot


[Link]()

Exercise 19:

Set the title of the plot to "Sports Watch Data" and add labels for the x- and y-axes.

Solution:

import numpy as np
import [Link] as plt

# Data points
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

# Plot the data


[Link](x, y)

# Add title and labels


[Link]("Sports Watch Data")
[Link]("Average Pulse")
[Link]("Calorie Burnage")

# Show the plot


[Link]()

Exercise 20:

Set the title font to blue with a size of 20 and the labels font to dark red with a size of 15.

Solution:

import numpy as np
import [Link] as plt
# Data points
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

# Font properties
font1 = {'family': 'serif', 'color': 'blue', 'size': 20}
font2 = {'family': 'serif', 'color': 'darkred', 'size': 15}

# Plot the data with custom fonts


[Link]("Sports Watch Data", fontdict=font1)
[Link]("Average Pulse", fontdict=font2)
[Link]("Calorie Burnage", fontdict=font2)

# Plot
[Link](x, y)
[Link]()

Exercise 21:

Move the title to the left of the plot.

Solution:

import numpy as np
import [Link] as plt

# Data points
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

# Plot the data with title positioned to the left


[Link]("Sports Watch Data", loc='left')
[Link]("Average Pulse")
[Link]("Calorie Burnage")

# Plot
[Link](x, y)
[Link]()

Title and Labels Functions:

 [Link]("Label"): Sets the label for the x-axis.


 [Link]("Label"): Sets the label for the y-axis.
 [Link]("Title"): Sets the title of the plot.

Font Properties:

 fontdict: A dictionary containing font properties (e.g., 'family', 'color', 'size').

Positioning the Title:

 loc: Specifies the position of the title. Options include:


o 'left'
o 'center' (default)
o 'right'
Exercise 22:

Plot the data and add grid lines to the plot.

Solution:

import numpy as np
import [Link] as plt

# Data points
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

# Plot the data


[Link]("Sports Watch Data")
[Link]("Average Pulse")
[Link]("Calorie Burnage")
[Link](x, y)

# Add grid lines


[Link]()

# Show the plot


[Link]()

Exercise 23:

Plot the data and display grid lines only on the x-axis.

Solution:

import numpy as np
import [Link] as plt

# Data points
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

# Plot the data


[Link]("Sports Watch Data")
[Link]("Average Pulse")
[Link]("Calorie Burnage")
[Link](x, y)

# Display grid lines only on the x-axis


[Link](axis='x')

# Show the plot


[Link]()

Exercise 24:

Plot the data and display grid lines only on the y-axis.

Solution:
import numpy as np
import [Link] as plt

# Data points
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

# Plot the data


[Link]("Sports Watch Data")
[Link]("Average Pulse")
[Link]("Calorie Burnage")
[Link](x, y)

# Display grid lines only on the y-axis


[Link](axis='y')

# Show the plot


[Link]()

Exercise 25:

Customize the grid lines by setting their color to green, linestyle to dashed, and width to 0.5.

Solution:

import numpy as np
import [Link] as plt

# Data points
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

# Plot the data


[Link]("Sports Watch Data")
[Link]("Average Pulse")
[Link]("Calorie Burnage")
[Link](x, y)

# Set the grid line properties


[Link](color='green', linestyle='--', linewidth=0.5)

# Show the plot


[Link]()
Exercise 26:

Create two plots using [Link]() and arrange them side by side in a 1x2 grid.

Solution:

import numpy as np
import [Link] as plt

# Plot 1
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](1, 2, 1) # 1 row, 2 columns, this is the first plot


[Link](x, y)

# Plot 2
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](1, 2, 2) # 1 row, 2 columns, this is the second plot


[Link](x, y)

[Link]()

Exercise 27:

Create two plots using [Link]() and arrange them in a 2x1 grid (2 rows, 1 column).

Solution:

import numpy as np
import [Link] as plt

# Plot 1
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](2, 1, 1) # 2 rows, 1 column, this is the first plot


[Link](x, y)

# Plot 2
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](2, 1, 2) # 2 rows, 1 column, this is the second plot


[Link](x, y)

[Link]()

Exercise 28:
Create 6 different plots, arranged in 2 rows and 3 columns.

Solution:

import numpy as np
import [Link] as plt

x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

# Plot 1
[Link](2, 3, 1)
[Link](x, y)

# Plot 2
y = [Link]([10, 20, 30, 40])
[Link](2, 3, 2)
[Link](x, y)

# Plot 3
y = [Link]([3, 8, 1, 10])
[Link](2, 3, 3)
[Link](x, y)

# Plot 4
y = [Link]([10, 20, 30, 40])
[Link](2, 3, 4)
[Link](x, y)

# Plot 5
y = [Link]([3, 8, 1, 10])
[Link](2, 3, 5)
[Link](x, y)

# Plot 6
y = [Link]([10, 20, 30, 40])
[Link](2, 3, 6)
[Link](x, y)

[Link]()

Exercise 29:

Create two plots side-by-side and add titles like "SALES" and "INCOME".

Solution:

import numpy as np
import [Link] as plt

# Plot 1
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](1, 2, 1)
[Link](x, y)
[Link]("SALES")

# Plot 2
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](1, 2, 2)
[Link](x, y)
[Link]("INCOME")

[Link]()

Exercise 30:

Add a super title "MY SHOP" for the whole figure with two subplots.

Solution:

import numpy as np
import [Link] as plt

# Plot 1
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](1, 2, 1)
[Link](x, y)
[Link]("SALES")

# Plot 2
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](1, 2, 2)
[Link](x, y)
[Link]("INCOME")

# Add super title


[Link]("MY SHOP")

[Link]()
Exercise 31:

Plot the given data points on a scatter plot.

Solution:

import [Link] as plt


import numpy as np

# Data points
x = [Link]([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = [Link]([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])

# Scatter plot
[Link](x, y)

# Show the plot


[Link]()

Exercise 32:

1. Plot the data for two days:


o Day 1: Age and speed of 13 cars.
o Day 2: Age and speed of 15 cars.

Solution:

import [Link] as plt


import numpy as np

# Day 1 data (13 cars)


x1 = [Link]([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y1 = [Link]([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])

# Day 2 data (15 cars)


x2 = [Link]([2, 2, 8, 1, 15, 8, 12, 9, 7, 3, 11, 4, 7, 14, 12])
y2 = [Link]([100, 105, 84, 105, 90, 99, 90, 95, 94, 100, 79, 112, 91, 80,
85])

# Scatter plot for both days


[Link](x1, y1, label='Day 1')
[Link](x2, y2, label='Day 2')

# Show the legend and plot


[Link]()
[Link]()

Exercise 33:

1. Set a custom color for each scatter plot:


o Day 1: Set the color to 'hotpink'.
o Day 2: Set the color to '#88c999'.
Solution:

import [Link] as plt


import numpy as np

# Day 1 data (13 cars)


x1 = [Link]([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y1 = [Link]([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])

# Day 2 data (15 cars)


x2 = [Link]([2, 2, 8, 1, 15, 8, 12, 9, 7, 3, 11, 4, 7, 14, 12])
y2 = [Link]([100, 105, 84, 105, 90, 99, 90, 95, 94, 100, 79, 112, 91, 80,
85])

# Scatter plot with custom colors


[Link](x1, y1, color='hotpink')
[Link](x2, y2, color='#88c999')

# Show the plot


[Link]()

Exercise 34:

1. Set a specific color for each dot:


o Use an array of colors to assign to each data point.

Solution:

import [Link] as plt


import numpy as np

# Data points
x = [Link]([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = [Link]([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
colors = [Link](["red", "green", "blue", "yellow", "pink", "black",
"orange", "purple", "beige", "brown", "gray", "cyan", "magenta"])

# Scatter plot with custom colors for each point


[Link](x, y, c=colors)

# Show the plot


[Link]()

Exercise 35:

1. Use a colormap to color the dots:


o Use a colormap 'viridis' and create a color array based on the values.

Solution:

import [Link] as plt


import numpy as np

# Data points
x = [Link]([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = [Link]([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
colors = [Link]([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
# Scatter plot with colormap
[Link](x, y, c=colors, cmap='viridis')

# Show the plot


[Link]() # Display the colormap
[Link]()

Exercise 36:

1. Set custom size and transparency for the dots:


o Use a custom array for dot sizes and set the transparency to 0.5.

Solution:

import [Link] as plt


import numpy as np

# Data points
x = [Link]([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = [Link]([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
sizes = [Link]([20, 50, 100, 200, 500, 1000, 60, 90, 10, 300, 600, 800,
75])

# Scatter plot with custom size and alpha (transparency)


[Link](x, y, s=sizes, alpha=0.5)

# Show the plot


[Link]()

Exercise 37:

1. Create a random scatter plot with color, size, and alpha:


o Use random values for the x, y, colors, and sizes, and combine them with
transparency.

Solution:

import [Link] as plt


import numpy as np

# Generate random data


x = [Link](100, size=(100))
y = [Link](100, size=(100))
colors = [Link](100, size=(100))
sizes = 10 * [Link](100, size=(100))

# Scatter plot with color, size, and transparency


[Link](x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')

# Show the colormap


[Link]()

# Show the plot


[Link]()

Functions Used
 [Link](x, y): Plots a scatter plot with x and y data.
 c=colors: Sets the color of each point in the scatter plot.
 s=sizes: Sets the size of each point in the scatter plot.
 alpha=0.5: Sets the transparency (0 is fully transparent, 1 is fully opaque).
 cmap='viridis': Specifies the colormap used to color the points.
 [Link](): Displays the colorbar for the colormap.
Exercise 38:

1. Create a vertical bar graph:


o Plot the given data for categories "A", "B", "C", and "D".

Solution:

import [Link] as plt


import numpy as np

# Data
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])

# Create bar plot


[Link](x, y)

# Show the plot


[Link]()

Exercise 39:

Plot the same data but as horizontal bars.

Solution:

import [Link] as plt


import numpy as np

# Data
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])

# Create horizontal bar plot


[Link](x, y)

# Show the plot


[Link]()

Exercise 40:

1. Set the color of the bars to "hotpink":


o Create a bar graph with the color set to "hotpink".

Solution:

import [Link] as plt


import numpy as np

# Data
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])

# Create bar plot with custom color


[Link](x, y, color="hotpink")
# Show the plot
[Link]()

Exercise 41:

1. Set the bar color to a beautiful green using a hex code:


o Plot the bars with the color set to #4CAF50.

Solution:

import [Link] as plt


import numpy as np

# Data
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])

# Create bar plot with hexadecimal color


[Link](x, y, color="#4CAF50")

# Show the plot


[Link]()

Exercise 42:

1. Set the width of the bars to 0.1:


o Create a bar graph with very thin bars.

Solution:

import [Link] as plt


import numpy as np

# Data
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])

# Create bar plot with custom width


[Link](x, y, width=0.1)

# Show the plot


[Link]()

Exercise 43:

1. Set the height of the horizontal bars to 0.1:


o Create a horizontal bar graph with very thin bars.

Solution:

import [Link] as plt


import numpy as np

# Data
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])

# Create horizontal bar plot with custom height


[Link](x, y, height=0.1)

# Show the plot


[Link]()

Bar Customization

 [Link](x, y): Creates a vertical bar plot with categories x and corresponding
values y.
 [Link](x, y): Creates a horizontal bar plot with categories x and corresponding
values y.
 color or c: Specifies the color of the bars (can use color names or hexadecimal
values).
 width: Adjusts the width of vertical bars.
 height: Adjusts the height of horizontal bars.
Exercise 1:

1. Create a histogram:
o Generate random data (250 values) with a normal distribution around 170 with
a standard deviation of 10. Plot the histogram.

Solution:

import [Link] as plt


import numpy as np

# Generate random data


x = [Link](170, 10, 250)

# Create the histogram


[Link](x)

# Show the plot


[Link]()

Exercise 2:

1. Set the number of bins to 20:


o Create the histogram and set the number of bins to 20 for better visualization
of data distribution.

Solution:

import [Link] as plt


import numpy as np

# Generate random data


x = [Link](170, 10, 250)

# Create the histogram with 20 bins


[Link](x, bins=20)

# Show the plot


[Link]()

Exercise 3:

1. Add a title and labels:


o Add a title "Height Distribution of 250 People", x-axis label "Height (cm)",
and y-axis label "Frequency".

Solution:

import [Link] as plt


import numpy as np

# Generate random data


x = [Link](170, 10, 250)

# Create the histogram


[Link](x, bins=20)

# Add title and labels


[Link]("Height Distribution of 250 People")
[Link]("Height (cm)")
[Link]("Frequency")

# Show the plot


[Link]()

Exercise 4:

1. Set the color of the bars to "skyblue":


o Customize the color of the histogram bars.

Solution:

import [Link] as plt


import numpy as np

# Generate random data


x = [Link](170, 10, 250)

# Create the histogram with custom bar color


[Link](x, bins=20, color='skyblue')

# Show the plot


[Link]()

Exercise 5:

1. Set the edge color of the bars to "black":


o Customize the histogram by setting the edge color to black for better clarity.

Solution:

import [Link] as plt


import numpy as np

# Generate random data


x = [Link](170, 10, 250)

# Create the histogram with black edges


[Link](x, bins=20, color='skyblue', edgecolor='black')

# Show the plot


[Link]()

Exercise 6:

1. Normalize the histogram:


o Create the histogram and normalize it by setting the density parameter to
True.

Solution:
import [Link] as plt
import numpy as np

# Generate random data


x = [Link](170, 10, 250)

# Create a normalized histogram


[Link](x, bins=20, density=True, color='skyblue', edgecolor='black')

# Show the plot


[Link]()

Exercise 7:

Set the y-axis to a logarithmic scale:

o Display the histogram with the y-axis on a logarithmic scale.

Solution:

import [Link] as plt


import numpy as np

# Generate random data


x = [Link](170, 10, 250)

# Create the histogram


[Link](x, bins=20, color='skyblue', edgecolor='black')

# Set y-axis to logarithmic scale


[Link]('log')

# Show the plot


[Link]()

Functions and Customizations:

 [Link](x, bins=20): Creates a histogram with x as the data and bins determining
the number of bins.
 density=True: Normalizes the histogram so that the area under the histogram sums to
1.
 color='color_name': Sets the color of the bars (e.g., 'skyblue', '#4CAF50').
 edgecolor='color_name': Sets the color of the bar edges.
 [Link]('log'): Changes the scale of the y-axis to logarithmic.
 [Link]('Label') and [Link]('Label'): Add labels to the x and y axes.
 [Link]('Title'): Adds a title to the histogram.

You might also like