Python Articles - Page 364 of 929

Program to count number of similar substrings for each query in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:15:35

410 Views

Suppose we have two strings s and a set of query Q. Where Q[i] contains pair (l, r), for each substring of s from l to r, we have to find number of substrings s from x to y where they are similar. Two strings s and t are similar if they follow these rules −They are of same lengthFor each pair of indices (i, j), if s[i] is same as s[j], then it must satisfy t[i] = t[j], and similarly if s[i] is not same as s[j], then t[i] and t[j] must be different.So, if the input is like ... Read More

Program to create a lexically minimal string from two strings in python

Arnab Chakraborty
Updated on 09-Oct-2021 11:29:37

199 Views

Suppose, we have two strings. We want to make a lexically minimum string from those strings. To make the string we compare the first letter of the two strings and extract the lexically smaller letter from one of the strings. In the case of a tie i.e, the letters are the same; we extract the letter from the first string. We repeat this process until both the strings are empty. The minimal string constructed has to be returned.So, if the input is like input_1 = 'TUTORIALS', input_2 = 'POINT', then the output will be POINTTUTORIALSIf we compare the two strings, ... Read More

Program to find out the number of shifts required to sort an array using insertion sort in python

Arnab Chakraborty
Updated on 09-Oct-2021 11:07:25

1K+ Views

Suppose we are given an array and asked to perform insertion sort on it. In insertion sort, each element in an array is shifted to its correct position in the array. We have to find out the total number of shifts required to sort an array. The total number of shifts is an integer number and if the array is already sorted, we return 0.So, if the input is like input_array = [4, 5, 3, 1, 2], then the output will be 8[4, 5, 3, 1, 2] = 0 shifts [4, 5, 3, 1, 2] = 0 shifts ... Read More

When is plt.Show() required to show a plot and when is it not?

Rishikesh Kumar Rishi
Updated on 09-Oct-2021 09:44:21

2K+ Views

plt.Show() would help whenever there is no interactive plot.fig.Show() would help to display all the figures if it is interactive.Let's take an example to observe the difference between plt.Show() and fig.Show().StepsOpen iPython shell.Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Plot a line using plot() method.Display the figure using Show() method.To display the figure, use Show() method with block=False.Exampleimport numpy as np from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create a new figure fig ... Read More

How to remove the axis tick marks on a Seaborn heatmap?

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:26:36

6K+ Views

To remove the axis tick marks on a Seaborn heatmap, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create random data points with 4×4 dimension.Plot the rectangular data as a color-encoded matrix.Use tick_params() for changing the appearance of ticks and tick labels. Use left=false and bottom=false to remove the tick marks.To display the figure, use Show() method.Exampleimport numpy as np import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(4, 4) ax = sns.heatmap(data, vmax=1) ax.tick_params(left=False, bottom=False) ... Read More

Make logically shading region for a curve in matplotlib

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:25:07

235 Views

To make logically shading region for a curve in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create t, s1 and s2 data points using numpy.Create a figure and a set of subplots.Plot t and s1 data points; add a horizontal line across the axis.Create a collection of horizontal bars spanning *yrange* with a sequence of xranges.Add a '~.Collection' to the axes' collections; return the collection.To display the figure, use Show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.collections as collections plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ... Read More

Saving a 3D-plot in a PDF 3D with Python

Rishikesh Kumar Rishi
Updated on 09-Oct-2021 09:38:47

5K+ Views

To save a 3D-plot in a PDF with Python, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Create u, v, x, y and z data points using numpy.Plot a 3D wireframe.Set the title of the plot.Save the current figure using savefig() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, projection='3d') u, v = np.mgrid[0:2 * np.pi:30j, ... Read More

Matplotlib – How to show the count values on the top of a bar in a countplot?

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:23:54

11K+ Views

To show the count values on the top of a bar in a countplot, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with one column.A countplot can be thought of as a histogram across a categorical, instead of a quantitative, variable.Iterate the returned axes of the countplot and show the count values at the top of the bars.To display the figure, use Show() method.Exampleimport seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ... Read More

How to control the border of a bar patch in matplotlib?

Rishikesh Kumar Rishi
Updated on 09-Oct-2021 09:35:09

6K+ Views

To control the border of a bar patch in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a list of heights and a tuple for labels.Use the bar() method with edgecolor in the argument to control the color of the bar patch. Here we have used edgecolor='green'.Set the ticks and labels of the X-axis.To display the figure, use Show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True height = [3, 12, 5, 18, 45] labels = ('P1', 'P2', 'P3', 'P4', ... Read More

How to increase the line thickness of a Seaborn Line?

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:21:02

4K+ Views

To increase the line thickness of a Seaborn line, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.Create a Seaborn line plot with linewidth value in the argument. Here we have set linewidth=7.Rotate the tick params, i.e., labels by 45 degrees.To display the figure, use Show() method.Exampleimport seaborn as sns from matplotlib import pyplot as plt import pandas as pd import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(     dict(     ... Read More

Advertisements