Found 26504 Articles for Server Side Programming

Which is the recommended way to plot – matplotlib or pylab?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:36:48

265 Views

pylab is a module that imports matplotlib.pyplot (for plotting) and numpy (for mathematics and working with arrays) in a single namespace.Although many examples use pylab, it is no longer recommended. For non-interactive plotting, it is suggested to use pyplot to create the figures and then the OO interface for plotting.Exampleimport matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2, 100) plt.plot(x, x, label='linear') plt.plot(x, x**2, label='quadratic') plt.plot(x, x**3, label='cubic') plt.xlabel('x label') plt.ylabel('y label') plt.title("Simple Plot") plt.legend() plt.show()Output

How to print array elements within a given range using Numpy?

Prasad Naik
Updated on 16-Mar-2021 10:41:54

837 Views

In this program, we have to print elements of a numpy array in a given range. The different numpy functions used are numpy.where() and numpy.logical_and().AlgorithmStep 1: Define a numpy array. Step 2: Use np.where() and np.logical_and() to find the numbers within the given range. Step 3: Print the result.Example Codeimport numpy as np arr = np.array([1,3,5,7,10,2,4,6,8,10,36]) print("Original Array:",arr) result = np.where(np.logical_and(arr>=4, arr

How to find set difference between two Numpy arrays?

Prasad Naik
Updated on 16-Mar-2021 10:41:10

11K+ Views

In this program, we will find the set difference of two numpy arrays. We will use the setdiff1d() function in the numpy library. This function takes two parameters: array1 and array2 and returns the unique values in array1 that are not in array2.AlgorithmStep 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find the set difference between these arrays using the setdiff1d() function. Step 4: Print the output.Example Codeimport numpy as np array_1 = np.array([2, 4, 6, 8, 10, 12]) print("Array 1: ", array_1) array_2 = np.array([4, 8, 12]) print("Array 2: ", array_2) set_diff = ... Read More

How to find intersection between two Numpy arrays?

Prasad Naik
Updated on 16-Mar-2021 10:37:11

3K+ Views

In this problem, we will find the intersection between two numpy arrays. Intersection of two arrays is an array with elements common in both the original arraysAlgorithmStep 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find intersection between the arrays using the numpy.intersect1d() function. Step 4: Print the array of intersecting elements.Example Codeimport numpy as np array_1 = np.array([1,2,3,4,5]) print("Array 1:", array_1) array_2 = np.array([2,4,6,8,10]) print("Array 2:", array_2) intersection = np.intersect1d(array_1, array_2) print("The intersection between the two arrays is:", intersection)OutputArray 1:  [1 2 3 4 5] Array 2:  [2  4  6  8 10] The intersection between the two arrays is:  [2 4]

How to change the tick size using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:31:50

3K+ Views

To change the tick size using ggplot2, we can use theme function with argument axis.ticks.length. For example, if we have a data frame called df that contains two columns say x and y then the scatterplot between x and y with larger size of tick marks can be created by using the below command −ggplot(df,aes(x,y))+geom_point()+theme(axis.ticks.length=unit(0.8,"inch"))ExampleConsider the below data frame − Live Demox

How to add a vector to a given Numpy array?

Prasad Naik
Updated on 16-Mar-2021 10:36:06

3K+ Views

 In this problem, we have to add a vector/array to a numpy array. We will define the numpy array as well as the vector and add them to get the result arrayAlgorithmStep 1: Define a numpy array. Step 2: Define a vector. Step 3: Create a result array same as the original array. Step 4: Add vector to each row of the original array. Step 5: Print the result array.Example Codeimport numpy as np original_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) print("Original Array: ", original_array) vector = np.array([1, 1, 0]) print("Vector: ... Read More

Change grid interval and specify tick labels in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:36:26

5K+ Views

Using plt.figure() method, we can create a figure and thereafter, we can create an axis. Using set_xticks and set_yticks, we can change the ticks format and ax.grid could help to specify the grid interval.StepsCreate a new figure, or activate an existing figure, using fig = plt.figure() method.Add an `~.axes.Axes` to the figure as part of a subplot arrangement, where nrow = 1, ncols = 1 and index = 1.Get or set the current tick locations and labels of the X-axis.Get or set the current tick locations and labels of the X-axis. With minor = True, Grid.Get or set the current ... Read More

How to find the sum of rows and columns of a given matrix using Numpy?

Prasad Naik
Updated on 16-Mar-2021 10:35:16

4K+ Views

In this problem, we will find the sum of all the rows and all the columns separately. We will use the sum() function for obtaining the sum.AlgorithmStep 1: Import numpy. Step 2: Create a numpy matrix of mxn dimension. Step 3: Obtain the sum of all the rows. Step 4: Obtain the sum of all the columns.Example Codeimport numpy as np a = np.matrix('10 20; 30 40') print("Our matrix: ", a) sum_of_rows = np.sum(a, axis = 0) print("Sum of all the rows: ", sum_of_rows) sum_of_cols = np.sum(a, axis = 1) print("Sum of all the columns: ", sum_of_cols)OutputOur ... Read More

Linear regression with Matplotlib/Numpy

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:35:45

9K+ Views

To get a linear regression plot, we can use sklearn’s Linear Regression class, and further, we can draw the scatter points.StepsGet x data using np.random.random((20, 1)). Return random floats in the half-open interval[20, 1).Get the y data using np.random.normal() method. Draw random samples from a normal (Gaussian) distribution.Get ordinary least squares Linear Regression, i.e., model.Fit the linear model.Return evenly spaced numbers over a specified interval, using linspace() method.Predict using the linear model, using predict() method.Create a new figure, or activate an existing figure, with a given figsize tuple (4, 3).Add an axis to the current figure and make it the ... Read More

What's the fastest way of checking if a point is inside a polygon in Python?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:33:54

5K+ Views

First, we will create a polygon using the mplPath.Path method and to check whether a given point is in the polygon or not, we will use the method, poly_path.contains_point.StepsCreate a list of points to make the polygon.Create a new path with the given vertices and codes, using mplPath.Path().Check if point (200, 100) exists in the polygon or not, using contains_point() method. Return whether the (closed) path contains the given point. => TrueCheck if point (1200, 1000) exists in the polygon or not, using contains_point() method. Return whether the (closed) path contains the given point. => FalseExampleimport matplotlib.path as mplPath import ... Read More

Advertisements