Introduction - NumPy Library

  • NumPy is a Python library that helps you work with numbers and arrays easily. It’s like a toolbox for doing mathematical operations on data.
  • NumPy, you can create arrays, which are collections of numbers. These arrays can have one or more dimensions, like rows and columns in a table.
  • Once you have an array, you can perform all sorts of mathematical operations on it. You can add, subtract, multiply, or divide arrays element-wise. This means each element in the array is operated on individually.
  • NumPy also offers functions for more advanced mathematical operations. You can calculate things like the sum, average, or standard deviation of the numbers in an array. There are also functions for trigonometry, logarithms, and more.
  • Another great feature of NumPy is indexing and slicing. This means you can easily access specific elements or subsets of elements in an array. It’s like picking out certain values from a table.
  • NumPy is widely used in scientific and data-related fields because it’s efficient and makes working with large amounts of data easier. It’s also the foundation for many other libraries in Python, such as SciPy and Pandas, which build upon NumPy’s capabilities.
  • To use NumPy, you just need to import it into your Python script or interactive session using the line import Numpy as np. Then, you can start using NumPy’s functions andcreate arrays to work with.

Array :

An array is a collection of items arranged in a specific order, where each item has an index. It allows you to work with multiple values together and perform operations on them efficiently.

Create an Array :

How to Check the NumPy Version:-

Create the User input Numpy array:-

Create array with user input - Take 5 input from user

Dimensions in Arrays :

A dimensional array is a structure created in the memory to represent a number of values of the same data type with the variables having the same variable name along with different subscripts.

Understanding the Types Dimensions of Arrays :-

 

Python supports various types of arrays, each catering to specific needs and use cases. Let’s explore the major type

One-Dimensional Arrays (1-D Arrays)

 

A one-dimensional (1-D) array is one linear collection of elements, organized in a single row or a single column. It is similar to a list, but with the added benefits of NumPy’s array functionality.

Example:

we can .ndim for check dimenision of array

Output

Example.

Create two array, but output should be in single dimension with add values element wise.

Output

Two-Dimensional Arrays (2D Arrays)

Extending the concept of a one-dimensional array, a two-dimensional array can be thought of as a matrix with rows and columns. It is essentially an array of arrays, providing a structured way to represent data that has two dimensions. This type of array is particularly useful when dealing with tables or grids of information.

Example.

Output.

Example.

Create a 2D Array with 3 rows and 3 columns

Output

Access the individual elements by using indexing. Example : To access element in the 2nd Row and 3rd Column

Indexing: In arrays, elements are accessed using indices, which represent their position in the array. Python follows zero-based indexing, where the first element is at index 0, the second at index 1, and so on.

Example.

Three-Dimensional Arrays (3D Arrays)

Taking complexity a step further, a three-dimensional array introduces an additional dimension, forming a cube of information. Think of it as an array of two-dimensional arrays. While less common, three-dimensional arrays find applications in scenarios where data has multiple layers of relationships.

Example.

Create a 3-D array with Nested list of Nested list. Each Nested list should be represent with 2-D array.

In this code we have created a 3-D array with a shape of (2,2,3), means it has “2 Layers”, each layers considering of 2X3 Matrix.

The nested lists [[1,2,3], [4,5,6]] and [[7,8,9], [10,11,12]] represent the two 2-D array or “Layers” within the 3-D array.

Example.

Access individual elements using indexing with an additional index for the “layer”. We want to access element in the 2nd layer, 1st row and 3rd column.

Example

Output

Higher Dimensional Arrays (4-D, 5-D, 10-D):

Higher-dimensional arrays is more than three dimensions. They are commonly used in scientific computing and data analysis for handling complex datasets.

Here is an Example:

Output

Create a 4-D array with Nested list of Nested list. Each Nested list should be represent with 2-D array.

In the above code, we created a 4-D array with a shape of (2, 2, 2, 2). It has four dimension, with “2 Layers”, each layer consisting of a 2X2 Matrix.

4D Array chart:

Access elements in higher-dimensional arrays using multiple indices, one for each dimension.

For example: To access the element in the 1st layer, 2nd matrix within the layer, 2nd row within the matrix and 2nd column within the matrix.

In this code, the index [0, 1, 1, 1] specifies:

1st layer (index 0),

2nd matrix within the layer (index 1),

2nd row with in the matrix (index 1)

**2nd col within the matrix (**index 1) of the array.

In this case, the indices specify the first layer (index 0), the second matrix within the layer (index 1), the second row within the matrix (index 1), and the second column within the matrix (index 1) of the array.

Special types of Arrays(Filled with specific values):-

Array filled with 0’s (create a 1-D array with five zero elements):-

Create a 2D array with 3 rows and 4 columns, all initialized to 0.

Create a 1D array with 5 elements, all initialized to 0 of integer type.

Array with Random Numbers

Rand() ———— Random values between 0 to 1: 

Another Example.

Randn() —— Random value close to zero  (Positive/Negative)

Renf() ———— renf method print the float values:-

Randint():- The randint() method takes a size parameter where you can specify the shape of an array:-

 Shapes & ReShape in Numpy:-

Shape

The shape tool gives a tuple of array dimensions and can be used to change the dimensions of an array.

Another Example

Reshape

The reshape tool gives a new shape to an array without changing its data. It creates a new array and does not modify the original array itself. 

Here is Example.

Here is Another Example.

Arithmetic operation in Numpy.

Numpy provides a wide rang of operations that can perform on arrays, including arithmetic operation.

Numpy’s arithmetic operation are widely used due to their ability to perform simple and efficient calculation on arrays.

  1. Addition

Add arrays element-wise using the + operator. 

Example 2.

np.add(a,b)

Output.

2. Subtraction

Subtraction element-wise the (-) Operator:-

np.subtract(a, b)

3. Multiplication

                     Element-wise multiplication is done using the (*) Operator:-

np.multiply(a, b).

4. Division

For element-wise division you can use the (/) operator. Be cautious with division by zero which can result in a runtime warning ‘inf’ value.

np.divide(a, b)

5. Power

The power() function rises the values from the first array to the power of the values of the second array, and return the results in a new array:-

np.power(a, b)

6. Modulus

Modulus operation(remainder of the division) is denoted by the (%) Operator:

np.mod(a, b)

 Arithmetic Function in Numpy:

Find out the Minimum value

np.min(vari)

 

Find out the Maximum value

np.max(vari)

Position No. of Min

np.argmin(vari)

Position No. of Maximum

np.argmax(vari)

Square Root

np.sqrt(vari)

Cumulative sum

np.cumsum(vari)

 

 

1. MIN:

For find Minimum Value, axis: 0 (Columns), axis: 1 (Rows):- 

Find minimum value in raw wise:

Output.

2. MAX :

For Find the maximum value

Find maximum values column wise

Output.

3. NP.ARGMIN: 

Find the position number of Minimum value

4. NP.ARGMAX:

Find the position number of Maximum value.

5. NP.SQRT(VARI):

Calculate the square root using the np.sqrt()

Calculate the square root of each element in the array

6. NP.CUMSUM(VARI):

Calculates the cumulative sum of elements along a given axis

Calculate the cumulative sum along axis 1 (columns) wise

Iterating Array

Iterating Array – Means Going through the elements one by one or step by step.

Iterate the element of 1-D

Iterate the element of 2-D

Iterate on each scalar element of the 3-D

NDITER( ) Function

Iterating arrays using the nditer() function,

Now we will Iterate on each scalar element.

Iterate 3-D

HERE IS SECOND EXAMPLE:

Iterate First Row

Iterate  2 Elements of Each Row

Iterate 3 Elements of Each Row

Iterate Elements of Each Row with Steps

Joining Array (Concatenate & Stack)

The process of combining the arrays into a single array or merging the arrays into a single array is known as Concatenation of arrays. This mechanism can be done in many ways using several techniques. Let us discuss all techniques that help in concatenation of arrays in Python.

 

Here for this we will pass the concatenate( )

Joining 2-D arrays along with rows (axis=1)

Stack function :

A stack is a linear data structure where data is arranged objects on over another. It stores the data in LIFO (Last in First Out) manner. The data is stored in a similar order as plates are arranged one above another in the kitchen. The simple example of a stack is the Undo feature in the editor.

 

Here is the Example:

Joining array using the stack function

HTACK( ) — Stacking along with rows

numpy.hstack() function is used to stack the sequence of input arrays horizontally (i.e. column wise) to make a single array.

 

Example

VTACK( ) — Stacking along with Columns

numpy.vstack() function is used to stack the sequence of input arrays vertically to make a single array.

 

Example.

DTACK( ) — Stacking along with depth (height)

The dstack() function in NumPy is used to stack or arrange the given arrays in a sequence depth wise (that is, along the third axis), thereby creating an array of at least 3-D.

Example.

Splitting Array

Splitting is reverse operation of Joining.

Joining merges multiple arrays into one and Splitting breaks one array into multiple.

We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.

 

Example.

Breaking the array, its working just opposite of contactenate

Array_Split( ) — Split in 3 Parts

Example 2.

Array_Split( ) — Split in 4 Parts

 

Array_Split( ) — Split into array with Index

Here is second example of Split array:

Array_Split( ) — Split the 2-D Array into Three 2-D Arrays

Sorting – Array

 

Sorting means putting elements in an ordered sequence.

Ordered sequence is any sequence that has an order corresponding to elements, like numeric or alphabetical, ascending or descending.

The NumPy ndarray object has a function called sort(), that will sort a specified array.

Example.

This will sort a specified array.

Sorting 1D – Sort the array

Sorting 2D Array

Filter Array:

Getting some elements out of an existing array and creating a new array is called filtering.

A boolean index list is a list of booleans corresponding to indexes in the array. If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array.

Example1.

Filter Array with TRUE/FALSE

Example2.

Filter Element Greater than 25 (>25)

Example3.

Create filter directly from array – Return only greater than 25. 

Example4.

Filter Array – Return only Even Value from the Array

Example5.

 Introduction to Matplotlib library: -

Matplotlib is a powerful plotting library in Python used for creating static, animated, and interactive visualizations. Matplotlib’s primary purpose is to provide users with the tools and functionality to represent data graphically, making it easier to analyze and understand. It was originally developed by John D. Hunter in 2003 and is now maintained by a large community of developers.

Key Features of Matplotlib:

1. Versatility: Matplotlib can generate a wide range of plots, including line plots, scatter plots, bar plots, histograms, pie charts, and more.

2. Customization: It offers extensive customization options to control every aspect of the plot, such as line styles, colors, markers, labels, and annotations.

3. Integration with NumPy: Matplotlib integrates seamlessly with NumPy, making it easy to plot data arrays directly.

4. Publication Quality: Matplotlib produces high-quality plots suitable for publication with fine-grained control over the plot aesthetics.

5. Extensible: Matplotlib is highly extensible, with a large ecosystem of add-on toolkits and extensions like Seaborn, Pandas plotting functions, and Basemap for geographical plotting.

6. Cross-Platform: It is platform-independent and can run on various operating systems, including Windows, macOS, and Linux.

7. Interactive Plots: Matplotlib supports interactive plotting through the use of widgets and event handling, enabling users to explore data dynamically.

What is a Matplotlib Figure?

In Matplotlib, a figure is the top-level container that holds all the elements of a plot. It represents the entire window or page where the plot is drawn.

Basic Components or Parts of Matplotlib Figure:-

The parts of a Matplotlib figure include (as shown in the figure above):

1. Figures in Matplotlib: The Figure object is the top-level container for all elements of the plot. It serves as the canvas on which the plot is drawn. You can think of it as the blank sheet of paper on which you’ll create your visualization.

2. Axes in Matplotlib: Axes are the rectangular areas within the figure where data is plotted. Each figure can contain one or more axes, arranged in rows and columns if necessary. Axes provide the coordinate system and are where most of the plotting occurs.

3. Axis in Matplotlib: Axis objects represent the x-axis and y-axis of the plot. They define the data limits, tick locations, tick labels, and axis labels. Each axis has a scale and a locator that determine how the tick marks are spaced.

4. Marker in Matplotlib: Markers are symbols used to denote individual data points on a plot. They can be shapes such as circles, squares, triangles, or custom symbols. Markers are often used in scatter plots to visually distinguish between different data points.

5. Adding lines to Figures: Lines connect data points on a plot and are commonly used in line plots, scatter plots with connected points, and other types of plots. They represent the relationship or trend between data points and can be styled with different colors, widths, and styles to convey additional information.

6. Matplotlib Title: The title is a text element that provides a descriptive title for the plot. It typically appears at the top of the figure and provides context or information about the data being visualized.

7. Axis Labels in Matplotlib: Labels are text elements that provide descriptions for the x-axis and y-axis. They help identify the data being plotted and provide units or other relevant information.

8. Ticks: Tick marks are small marks along the axis that indicate specific data points or intervals. They help users interpret the scale of the plot and locate specific data values.

9. Tick Labels: Tick labels are text elements that provide labels for the tick marks. They usually display the data values corresponding to each tick mark and can be customized to show specific formatting or units.

10. Matplotlib Legend: Legends provide a key to the symbols or colors used in the plot to represent different data series or categories. They help users interpret the plot and understand the meaning of each element.

11. Matplotlib Grid Lines: Grid lines are horizontal and vertical lines that extend across the plot, corresponding to specific data intervals or divisions. They provide a visual guide to the data and help users identify patterns or trends.

12. Spines of Matplotlib Figures: Spines are the lines that form the borders of the plot area. They separate the plot from the surrounding whitespace and can be customized to change the appearance of the plot borders.

Different Types of Plots in Matplotlib:-

Matplotlib offers a wide range of plot types to suit various data visualization needs. Here are some of the most commonly used types of plots in Matplotlib:

· Line Graph

· Stem Plot

· Bar chart

· Histograms

· Scatter Plot

· Stack Plot

· Box Plot

· Pie Chart

· Error Plot

· Violin Plot

· 3D Plots 

 

 

Exploring Different Plot Styles with Matplotlib:-

Matplotlib’s built-in styles include classic styles reminiscent of traditional scientific plots, modern styles with vibrant colors and sleek lines, and specialized styles tailored for specific purposes such as presentation or grayscale printing. Additionally, Matplotlib allows you to customize plot styles to match your preferences or corporate branding, ensuring that your visualizations are both informative and visually appealing.

· Python Pyplot

· Matplotlib Figure Class

· Matplotlib Axes Class

· Set Colors in Matplotlib

· Add Text, Font and Grid lines in Matplotlib

· Custom Legends with Matplotlib

· Matplotlib Ticks and Tick Labels

· Style Plots using Matplotlib

· Create Multiple Subplots in Matplotlib

· Working With Images In Matplotlib

Advantages of Matplotlib:-

Matplotlib is a widely used plotting library in Python that provides a variety of plotting tools and capabilities. Here are some of the advantages of using Matplotlib:

1. Versatility: Matplotlib can create a wide range of plots, including line plots, scatter plots, bar plots, histograms, pie charts, and more.

2. Customization: It offers extensive customization options to control every aspect of the plot, such as line styles, colors, markers, labels, and annotations.

3. Integration with NumPy: Matplotlib integrates seamlessly with NumPy, making it easy to plot data arrays directly.

4. Publication Quality: Matplotlib produces high-quality plots suitable for publication with fine-grained control over the plot aesthetics.

5. Wide Adoption: Due to its maturity and flexibility, Matplotlib is widely adopted in the scientific and engineering communities.

6. Extensible: Matplotlib is highly extensible, with a large ecosystem of add-on toolkits and extensions like Seaborn, Pandas plotting functions, and Basemap for geographical plotting.

7. Cross-Platform: It is platform-independent and can run on various operating systems, including Windows, macOS, and Linux.

8. Interactive Plots: Matplotlib supports interactive plotting through the use of widgets and event handling, enabling users to explore data dynamically.

9. Integration with Jupyter Notebooks: Matplotlib works seamlessly with Jupyter Notebooks, allowing for interactive plotting and inline display of plots.

10. Rich Documentation and Community Support: Matplotlib has comprehensive documentation and a large community of users and developers, making it easy to find help, tutorials, and examples.

Disadvantages of Matplotlib:-

While Matplotlib is a powerful and versatile plotting library, it also has some disadvantages that users might encounter:

1. Steep Learning Curve: For beginners, Matplotlib can have a steep learning curve due to its extensive customization options and sometimes complex syntax.

2. Verbose Syntax: Matplotlib’s syntax can be verbose and less intuitive compared to other plotting libraries like Seaborn or Plotly, making it more time-consuming to create and customize plots.

3. Default Aesthetics: The default plot aesthetics in Matplotlib are often considered less visually appealing compared to other libraries, requiring more effort to make plots visually attractive.

4. Limited Interactivity: While Matplotlib does support interactive plotting to some extent, it does not offer as many interactive features and options as other libraries like Plotly.

5. Limited 3D Plotting Capabilities: Matplotlib’s 3D plotting capabilities are not as advanced and user-friendly as some other specialized 3D plotting libraries.

6. Performance Issues with Large Datasets: Matplotlib can sometimes be slower and less efficient when plotting large datasets, especially compared to more optimized plotting libraries.

7. Documentation and Error Messages: Although Matplotlib has comprehensive documentation, some users find it challenging to navigate, and error messages can sometimes be cryptic and hard to debug.

8. Dependency on External Libraries: Matplotlib relies on other libraries like NumPy and SciPy for many of its functionalities, which can sometimes lead to compatibility issues and dependency management issues.

9. Limited Native Support for Statistical Plotting: While Matplotlib can create basic statistical plots, it lacks some advanced statistical plotting capabilities that are available in specialized libraries like Seaborn.

10. Less Modern Features: Matplotlib has been around for a long time, and some users find that it lacks some of the modern plotting features and interactive visualization capabilities found in newer libraries.

 

Matplotlib is a versatile and powerful library for creating high-quality plots and visualizations in Python. With its extensive customization options and wide range of plotting capabilities, it is widely used in the scientific, engineering, and data science communities for data exploration, analysis, and presentation.

Syntax:- import matplotlib.pyplot as plt. Then you can start using matplotlib library.

 

Install Matplotlib

If you haven’t installed Matplotlib yet, you can do so using the following command:

 

Here is the Sample Example :

Output:

Mfc:- Matplotlib Face colour

Mec:- Matplotlib Edge colour

Ls:- linestyle

Lw:- LineWidth

C :- Colour 

Basic line plot with title:-

Output:

Plot with Title/X-Axis/Y-Axis

Output:

Plot with marker /Mec/Mfc :

Output :

Plot with Marker , Mec, Mfc, ls, lw, c :-

Output:

Plot with Gridlines:

Output:

Horizontal Grid :

Output:

Horizontal & Vertical Both:

Output:

dotted grid chart:

Output:

Grid chart:

Output:

Plot with Marker Formatting:

Display a marker at each data point by including the line-specification input argument when calling the plot function. For example, use ‘-o’ for a solid line with circle markers. If you specify a marker symbol and do not specify a line style, then plot displays only the markers with no line connecting them.

Marker Reference

You can choose any of these markers:

Marker Example :

Output:

Uses of basic line plot charts:

 

Show Trends Over Time:

Output:

Connect Data Points:

Output:

Highlight Patterns:

Output:

Compare Multiple Series:

Output:

Sub Plot:

A line plot is one of the most common types of charts used in data visualization. It’s used to represent data points using lines to connect them. Line plots are useful for displaying data trends over a continuous interval or time series.

 

Sub plot(Vertical Example):

Output:

Sub plot(Horizontal Example): 

Output:

Scatter Plot:

A scatter plot is a diagram where each value in the data set is represented by a dot. The Matplotlib module has a method for drawing scatter plots, it needs two arrays of the same length, one for the values of the x-axis, and one for the values of the y-axis: x = [5,7,8,7,2,17,2,9,4,11,12,9,6]

 

he 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:

 

Here is the Example:

Output:

Here is the second Example:

Plotting the two point in Scatter Sales and profit:

Output:

There are several marker styles available in Matplotlib. Here are some commonly used ones:

Multicolour plotting in Scatter:

Example:

Output:

Custom colour Plotting in Scatter plot:

Example:

Output:

Xticks, yticks, xlabel, xlim, ylim methods:

It is generally a number/list or in rare cases it can be a string. While plotting the graph the axes adjusts and takes the values of ticks. xticks() and yticks() are functions in matplotlib that take a list values as arguments. example: from matplotlib import pyplot as plt.

 

Here is the Example of (Xticks and Yticks):

Output: 

Here is the Example of(Xlim or Ylim):

Output:

Bar Chart :

Bar plots are a type of data visualization used to represent data in the form of rectangular bars. The height of each bar represents the value of a data point, and the width of each bar represents the category of the data. The Matplotlib library in Python is widely used to create bar plots.

Bar Chart (Vertical/Horizontal Bar Plot):

Output:

Horizontal Bar chart:

Output:

Colours in Matplotlib:

In Matplotlib, you can specify colours in a variety of ways, allowing for a great deal of flexibility in customizing the appearance of your charts. Here are some common ways to specify colours:

 

String Name Colours:

Matplotlib recognizes a variety of colour names such as ‘red’, ‘blue’, ‘green’, etc.

blue

green

red

cyan

magenta

yellow

black

white

 Ø String abbreviation

You can use single-character abbreviations for basic colors, like ‘b’ for blue, ‘g’ for green, etc.

‘b’: blue

‘g’: green

‘r’: red

‘c’: cyan

‘m’: magenta

‘y’: yellow

‘k’: black

‘w’: white

Pie Plot:

Pie charts show the size of items (called wedge) in one data series, proportional to the sum of the items. The data points in a pie chart are shown as a percentage of the whole pie. Matplotlib API has a pie() function that generates a pie diagram representing data in an array.

Here is the Example of pie Plot:

Output: