Dynamically Update Plots in Jupyter IPython

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

562 Views

We can first activate the figure using plt.ion() method. Then, we can update the plot with different sets of values.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Draw a line, using plot() method.Set the color of line, i.e., orange.Activate the interaction, using plt.ion() method.To make the plots interactive, change the line coordinates.ExampleIn [1]: %matplotlib auto Using matplotlib backend: GTK3Agg In [2]: import matplotlib.pyplot as plt # Diagram will get popped up. Let’s update the diagram. In [3]: fig, ax = plt.subplots() # Drawing a line In [4]: ax.plot(range(5)) In ... Read More

Reset Index in Pandas DataFrame

Prasad Naik
Updated on 16-Mar-2021 10:14:44

415 Views

In this program, we will replace or, in other words, reset the default index in the Pandas dataframe. We will first make a dataframe and see the default index and then replace this default index with our custom index.AlgorithmStep 1: Define your dataframe. Step 2: Define your own index. Step 3: Replace the default index with your index using the reset function in Pandas library.Example Codeimport pandas as pd dataframe = {'Name':["Allen", "Jack", "Mark", "Vishal"], 'Marks':[85, 92, 99, 87]} df = pd.DataFrame(dataframe) print("Before using reset_index:", df) own_index = ['a', 'j', 'm', 'v'] df = pd.DataFrame(dataframe, own_index) ... Read More

Make Several Plots on a Single Page Using Matplotlib in Python

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

411 Views

Using Pandas, we can create a data frame and create a figure and axis. After that, we can use the scatter method to draw points.StepsCreate lists of students, marks obtained by them, and color codings for each score.Make a data frame using Panda’s DataFrame, with step 1 data.Create fig and ax variables using subplots method, where default nrows and ncols are 1.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.A scatter plot of *y* vs. *x* with varying marker size and/or color.To show the figure, use plt.show() method.Examplefrom matplotlib import pyplot as plt import pandas as ... Read More

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

Defining the Midpoint of a Colormap in Matplotlib

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

387 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

Print Dates of Today, Yesterday, and Tomorrow using Numpy

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

467 Views

In this program, we will print today's, yesterday's and tomorrow's dates using the numpy library.AlgorithmStep 1: Import the numpy library. Step 2: find today's date using the datetime64() function. Step 3: find yesterday's date by subtracting the output of timedelta64() function from the output of datetime64() function. Step 4: Find yesterday's date by adding the output of timedelta64() function from the output of datetime64() function.Example Codeimport numpy as np todays_date = np.datetime64('today', 'D') print("Today's Date: ", todays_date) yesterdays_date = np.datetime64('today', 'D') - np.timedelta64(1, 'D') print("Yesterday's Date: ", yesterdays_date) tomorrows_date = np.datetime64('today', 'D') + np.timedelta64(1, 'D') print("Tomorrow's Date: ... Read More

Convert Negative Values in a Matrix to 0 in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:10:24

5K+ Views

To convert negative values in a matrix to 0, we can use pmax function. For example, if we have a matrix called M that contains some negative and some positive and zero values then the negative values in M can be converted to 0 by using the command pmax(M,0).ExampleConsider the below data frame − Live DemoM1

Draw Different Shapes Using the Python Turtle Library

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

3K+ Views

In this program, we will draw different shapes using the Turtle library in Python. Turtle is a python feature like a drawing board, which lets you command a turtle to draw all over it. The different shapes that we are going to draw are square, rectangle, circle and a hexagon.AlgorithmStep 1: Take lengths of side for different shapes as input.Step 2: Use different turtle methods like forward() and left() for drawing different shapes.Example Codeimport turtle t = turtle.Turtle() #SQUARE side = int(input("Length of side: ")) for i in range(4):    t.forward(side)    t.left(90) #RECTANGLE side_a = int(input("Length of ... Read More

Set the Backend in Matplotlib for Python

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:08:06

722 Views

We can use matplotlib.rcParams['backend'] to override the backend value.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

Scatter Plot and Color Mapping in Python

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

766 Views

We can create a scatter plot using the scatter() method and we can set the color for every data point.StepsCreate random values (for x and y) in a given shape, using np.random.rand() method.Create a scatter plot of *y* vs. *x* with varying marker size and/or color, using the scatter method where color range would be in the range of (0, 1000).Show the figure using plt.show().Exampleimport matplotlib.pyplot as plt import numpy as np x = np.random.rand(1000) y = np.random.rand(1000) plt.scatter(x, y, c=[i for i in range(1000)]) plt.show()OutputRead More

Advertisements