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
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
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
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
In this program, we will add all the terms of a numpy matrix using the sum() function in the numpy library. We will first create a random numpy matrix and then, we will obtain the sum of all the elements.AlgorithmStep 1: Import numpy. Step 2: Create a random m×n matrix using the random() function. Step 3: Obtain the sum of all the elements in the matrix using the sum() function.Example Codeimport numpy as np matrix = np.random.rand(3, 3) print("The numpy matrix is: ", matrix) print("The sum of the matrix is: ", np.sum(matrix))OutputThe numpy matrix is: [[0.66411969 0.43672579 0.48448593] [0.76110384 ... Read More
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
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
If we have a data frame that contains a character column and a named vector which has the same names as in the character column of the data frame then we can combine this data frame and the vector by using match function be appropriately defining the names and the character column. Check out the below example to understand how it can be done.ExampleConsider the below data frame df1 and the vector v1 − Live Demodf1
First, we can initialize an array matrix and pass it into the imshow method that can help to get the image for the given matrix.StepsCreate a 2D Array i.e., img.Using imshow() method, display the data as an image, i.e., on a 2D regular raster.Use plt.show() method to show the figure.Exampleimport matplotlib.pyplot as plt img = [[1, 2, 4, 5, 6, 7], [11, 12, 14, 15, 16, 17], [101, 12, 41, 51, 61, 71], [111, 121, 141, 151, 161, 171]] plt.imshow(img, extent=[0, 5, 0, 5]) plt.show()Output
To make a custom color, we can create a hexadecimal string. From it, we can make different sets of color representation and can pass into the scatter method to get the desired output.StepsTake an input from the user for the number of colors, i.e., number_of_colors = 20.Use Hexadecimal alphabets to get a color.Create a color from (step 2) by choosing a random character from step 2 data.Plot scatter points for step 1 input data, with step 3 colors.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt import random number_of_colors = int(input("Please enter number of colors: ")) hexadecimal_alphabets ... Read More