String Acceptance and Rejection by NPDA

Bhanu Priya
Updated on 15-Jun-2021 13:25:44

412 Views

A string is accepted by an Non-deterministic Push down Automata (NPDA), if there is some path (i.e., sequence of instructions) from the start state to a final state that consumes all the letters of the string. Otherwise, the string is rejected by the NPDA.The language of an NPDA is the set of all strings that it accepts.An input string rejected by the NPDA under following conditions −If reading an input string finishes without reaching a final state.If for a current state/symbol on the stack/input symbol there is no transition.If it attempts to pop the empty stack.ExampleBuild an NPDA which recognises ... Read More

Display Count Over Bar in Matplotlib Histogram

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 13:19:36

4K+ Views

To display the count over the bar in matplotlib histogram, we can iterate each patch and use text() method to place the values over the patches.StepsSet the figure size and adjust the padding between and around the subplots.Make a list of numbers to make a histogram plot.Use hist() method to make histograms.Iterate the patches and calculate the mid-values of each patch and height of the patch to place a text.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = [3, 5, 1, 7, 9, 5, 3, 7, 5] _, ... Read More

Replace Auto-Labelled Relative Values with Absolute Values in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 13:18:13

777 Views

To replace auto-labelled relayive values by absolute values in matplotlib, we can use autopct=lambda p: .StepsSet the figure size and adjust the padding between and around the subplots.Make lists of labels, fractions, explode position and get the sum of fractions to calculate the percentage.Make a pie chart using labels, fracs and explode with autopct=lambda p: .To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True labels = ('Read', 'Eat', 'Sleep', 'Repeat') fracs = [5, 3, 4, 1] total = sum(fracs) explode = (0, 0.05, 0, 0) plt.pie(fracs, explode=explode, labels=labels,   ... Read More

Make Simple Double Head Arrows on the Axes in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 13:17:12

4K+ Views

To make simple double head arrows on the axes in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Use annotate() method to annotate the point xy with text='Arrows'. Start the tuple and end it for positions. In arrowprops dictionary, use arrowstyle "" and color='red'.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.annotate('Arrows', xy=(0.1, .1), xytext=(0.5, 0.5),             arrowprops=dict(arrowstyle='', color='red')) plt.show()Output

Add Legends and Title to Grouped Histograms in Pandas Matplotlib

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 13:16:24

4K+ Views

To add legends and title to grouped histograms generated by Pandas, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with "a", "b", "c" and "d" keys.Plot data frame with kind="hist"Set a title for the axes.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'a': [1, 1, 1, 1, 3],                   'b': [1, 1, 2, 1, 3],                   'c': [2, 2, 2, 1, 3],                   'd': [2, 1, 2, 1, 3], }) df.plot(kind='hist') plt.title("Grouped Histograms") plt.show()Output

Plot Scatter Points on Polar Axis in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 13:15:37

1K+ Views

To plot scatter points on polar axis in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable, N, for number of sample data.Get r, theta, area and color data using numpyCreate a new figure or activate an existing figure.Plot theta, r, colors and area, using scatter() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True N = 150 r = 2 * np.random.rand(N) theta = 2 * np.pi * np.random.rand(N) area = 200 ... Read More

Omit Matplotlib Printed Output in Python Jupyter Notebook

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 13:14:50

3K+ Views

To omit matplotlib printed output in Python/Jupeter notebook, we can take the following steps −import numpy as np.from matplotlib import pyplot as pltCreate points for x, i.e., np.linspace(1, 10, 1000)Now, plot the line using plot() method.To hide the instance, use plt.plot(x); (with semi-colon)Or, use _ = plt.plot(x).ExampleIn [1]: import numpy as np In [2]: from matplotlib import pyplot as plt In [3]: x = np.linspace(1, 10, 1000) In [4]: plt.plot(x) Out[4]: [] In [5]: plt.plot(x); In [6]: _ = plt.plot(x) In [7]:OutputOut[4]: []

Save Figures to PDF as Raster Images in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 13:12:21

1K+ Views

To save figures to pdf as raster images in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an axes to the figure as part of a subplot arrangement.Create random data using numpy.Display the data as an image, i.e., on a 2D regular raster.Save the plot as pdf format.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, rasterized=True) data = np.random.rand(5, 5) ax.imshow(data, cmap="copper", aspect=True, interpolation="nearest") ... Read More

Restrictions of Regular Grammar

Bhanu Priya
Updated on 15-Jun-2021 13:11:38

1K+ Views

A regular grammar is the one where each production takes one of the following restricted forms −B → ∧, B → w, B → A, B → wA.(Where A, B are non-terminals and w is a non-empty string of terminals.)Restrictions of regular grammarOnly one nonterminal can appear on the right-hand side of a production.Nonterminal must appear on the right end of the right-hand side.Therefore, the productions are as follows −A → aBc and S → TUThese are not part of a regular grammar, but the production A → abcA is.Things like A → aB|cC are allowed because they are actually ... Read More

Get Color of Last Figure in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 13:04:01

3K+ Views

To get the color of the last figure, we can use get_color() method for every plot.Set the figure size and adjust the padding between and around the subplots.Create x and y data point using numpy.Plot (x, x), (x, x2) and (x, x3) using plot() method.Place a legend for every plot line.Get the color of each plot using get_color() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(10) y = np.arange(10) p = plt.plot(x, y, x, y ** 2, x, y ** 3) ... Read More

Advertisements