Found 10476 Articles for Python

Print dates of today, yesterday and tomorrow using Numpy

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

469 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

How to make several plots on a single page using matplotlib in Python?

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

417 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

How can I set the 'backend' in matplotlib in Python?

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

723 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

How to reset index in Pandas dataframe?

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

418 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

How to plot two dotted lines and set marker using Matplotlib?

Prasad Naik
Updated on 16-Mar-2021 10:15:42

2K+ Views

In this program, we will plot two lines using the matplot library. Before starting to code, we need to first import the matplotlib library using the following command −Import matplotlib.pyplot as pltPyplot is a collection of command style functions that make matplotlib work like MATLAB.AlgorithmStep 1: Import matplotlib.pyplot Step 2: Define line1 and line2 points. Step 3: Plot the lines using the plot() function in pyplot. Step 4: Define the title, X-axis, Y-axis. Step 5: Display the plots using the show() function.Example Codeimport matplotlib.pyplot as plt line1_x = [10, 20, 30] line1_y = [20, 40, 10] line2_x = ... Read More

How to 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

Python program to compare two Pandas series

Prasad Naik
Updated on 16-Mar-2021 09:55:48

604 Views

In this program, we will declare two Pandas series and compare their elements. Before we solve the problem, we need to import the Pandas library into our local IDE. This can be done by installing Pandas on our local machine. The command for installing Pandas is −pip install pandasInputSeries1 = [2,4,6,8,10]Series2 = [1,3,5,7,9]AlgorithmStep 1: Define two Pandas series using the Series() function of Pandas library.Step 2: Compare the series using greater than, less than, and equal-to operators.Example Codeimport pandas as pd series1 = pd.Series([2,4,6,8,10]) series2 = pd.Series([1,3,5,7,9]) print("Greater Than: ",series1>series2) print("Less Than: ",series1

How to change fonts in matplotlib (python)?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 08:42:43

2K+ Views

Using plt.text() method, we can increase the font size.StepsUsing plt.plot() method, we can create a line with two lists that are passed in its argument.Add text to the axes. Add the text *s* to the axes at location *x*, *y* in data coordinates, using plt.text() method. Font size can be customized by changing the font-size value.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt plt.plot([1, 2, 4], [1, 2, 4]) plt.text(2, 3, "y=x", color='red', fontsize=20) # Increase fontsize by increasing value. plt.show()Output

How to maximize a plt.show() window using Python?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 08:04:38

5K+ Views

Using plt.get_current_fig_manager() and mng.full_screen_toggle() methods, we can maximise a plot.StepsAdd a subplot to the current figure, where nrow = 1, ncols = 1 and index = 1.Create a pie chart using list [1, 2, 3] and pie() method.Return the figure manager of the current figure, using get_current_fig_manager() method. The figure manager is a container for the actual backend-depended window that displays the figure on the screen.Create an abstract base class to handle drawing/rendering operations using the full_screen_toggle() method.Use plt.show() to show the figure.Exampleimport matplotlib.pyplot as plt plt.subplot(1, 1, 1) plt.pie([1, 2, 3]) mng = plt.get_current_fig_manager() mng.full_screen_toggle() plt.show()OutputRead More

Advertisements