Found 33676 Articles for Programming

How to change backends in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:18:47

2K+ Views

We can override the backend value using atplotlib.rcParams['backend'] variable.StepsUsing get_backend() method, return the name of the current backend, i.e., default name.Now override the backend name.Using get_backend() method, return the name of the current backend, i.e., updated name.Exampleimport matplotlib print("Before, Backend used by matplotlib is: ", matplotlib.get_backend()) matplotlib.rcParams['backend'] = 'TkAgg' print("After, Backend used by matplotlib is: ", matplotlib.get_backend())OutputBefore, Backend used by matplotlib is: GTK3Agg After, Backend used by matplotlib is: TkAgg Enter number of bars: 5

How to give a Pandas/Matplotlib bar graph custom colors?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:19:16

4K+ Views

To make a custom color, we can create a hexadecimal string. From it, we can make different sets of color representation and can pass them into the scatter method to get the desired output.Using the set_color method, we could set the color of the bar.StepsTake user input for the number of bars.Add bar using plt.bar() method.Create colors from hexadecimal alphabets by choosing random characters.Set the color for every bar, using set_color() method.To show the figure we can use plt.show() method.Examplefrom matplotlib import pyplot as plt import random bar_count = int(input("Enter number of bars: ")) bars = plt.bar([i for ... Read More

Finding the number of rows and columns in a given matrix using Numpy

Prasad Naik
Updated on 16-Mar-2021 10:20:19

3K+ Views

We will first create a numpy matrix and then find out the number of rows and columns in that matrixAlgorithmStep 1: Create a numpy matrix of random numbers. Step 2: Find the rows and columns of the matrix using numpy.shape function. Step 3: Print the number of rows and columns.Example Codeimport numpy as np matrix = np.random.rand(2,3) print(matrix) print("Total number of rows and columns in the given matrix are: ", matrix.shape)Output[[0.23226052 0.89690884 0.19813164]  [0.85170808 0.97725669 0.72454096]] Total number of rows and columns in the given matrix are:  (2, 3)

How to create an identity matrix using Numpy?

Prasad Naik
Updated on 16-Mar-2021 10:20:45

4K+ Views

In this program, we will print an identity matrix of size nxn where n will be taken as an input from the user. We shall use the identity() function in the numpy library which takes in the dimension and the data type of the elements as parametersAlgorithmStep 1: Import numpy. Step 2: Take dimensions as input from the user. Step 3: Print the identity matrix using numpy.identity() function.Example Codeimport numpy as np dimension = int(input("Enter the dimension of identitiy matrix: ")) identity_matrix = np.identity(dimension, dtype="int") print(identity_matrix)OutputEnter the dimension of identitiy matrix: 5 [[1 0 0 0 0]  [0 1 0 0 0]  [0 0 1 0 0]  [0 0 0 1 0]  [0 0 0 0 1]]

How to create a numpy array within a given range?

Prasad Naik
Updated on 16-Mar-2021 10:21:08

1K+ Views

We have to create a numpy array in the range provided by the user. We will use the arange() function in the numpy library to get our output.AlgorithmStep1: Import numpy. Step 2: Take start_value, end_value and Step from the user. Step 3: Print the array using arange() function in numpy.Example Codeimport numpy as np start_val = int(input("Enter starting value: ")) end_val = int(input("Enter ending value: ")) Step_val = int(input("Enter Step value: ")) print(np.arange(start_val, end_val, Step_val))OutputEnter starting value: 5 Enter ending value: 50 Enter Step value: 5 [ 5 10 15 20 25 30 35 40 45]

How to generate random colors in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:21:32

3K+ Views

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

Change values on matplotlib imshow() graph axis

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:21:58

4K+ Views

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

Defining the midpoint of a colormap in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:11:07

389 Views

Using plt.subplots(1, 1) method, we can create fig and axis. We can use fig.colorbar to make the color bar at the midpoint of the figure.StepsUsing mgrid() method, `nd_grid` instance which returns an open multi-dimensional "meshgrid".Create Z1, Z2 and Z data.Create fig and ax variables using subplots method, where default nrows and ncols are 1, using subplots() method.Create a colorbar for a ScalarMappable instance, *mappable*, using colorbar() method.Using plt.show(), we can show the figure.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.colors as colors N = 100 X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] Z1 = np.exp(-(X)**2 - ... Read More

How to plot ROC curve in Python?

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

7K+ Views

ROC − Receiver operating characteristics (ROC) curve.Using metrics.plot_roc_curve(clf, X_test, y_test) method, we can draw the ROC curve.StepsGenerate a random n-class classification problem. This initially creates clusters of points normally distributed (std=1) about vertices of an ``n_informative``-dimensional hypercube with sides of length ``2*class_sep`` and assigns an equal number of clusters to each class.It introduces interdependence between these features and adds various types of further noise to the data. Use the make_classification() method.Split arrays or matrices into random trains, using train_test_split() method.Fit the SVM model according to the given training data, using fit() method.Plot Receiver operating characteristic (ROC) curve, using plot_roc_curve() method.To ... Read More

Automatically run %matplotlib inline in IPython Notebook

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 20:15:30

304 Views

%matplotlib would return the backend value.%matplotlib auto would return the name of the backend, over Ipython shell.ExampleIn [1]: %matplotlib autoOutputUsing matplotlib backend: GTK3Agg

Advertisements