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

Customize Edge Labels Display in NetworkX using Matplotlib

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

3K+ Views

To set the networkx edge labels offset, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a graph with edges, name, or graph attributes.Add multiple nodes.Position the nodes using Fruchterman-Reingold force-directed algorithm.Draw the graph G with Matplotlib.Draw edge labels.To display the figure, use show() method.Exampleimport matplotlib.pylab as plt import networkx as nx plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True G = nx.DiGraph() G.add_nodes_from([1, 2, 3, 4]) G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 1), (1, 3)]) pos = nx.spring_layout(G) for u, v, d in G.edges(data=True): d['weight'] ... Read More

Plot Contourf and Log Color Scale in Matplotlib

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

2K+ Views

To plot contourf and log scale 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.Create x, y, X, Y, Z1, Z2 and z data points using numpy.Create a figure and a set of subplots.Plot contours using contourf() method.Create a colorbar for a scalar mappable instance.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np from numpy import ma from matplotlib import ticker, cm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True N = 100 x ... Read More

Independently Set Horizontal and Vertical Gridlines of a Plot

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:54:15

994 Views

To set horizontal and vertical, major and minor grid lines of a plot, we can use grid() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make horizontal grid lines for major ticks.Locate minor locator on the axes.Use grid() method to make minor grid lines.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from matplotlib.ticker import MultipleLocator plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() ax.yaxis.grid(which="major", color='r', linestyle='-', linewidth=2) ml = MultipleLocator(0.10) ax.xaxis.set_minor_locator(ml) ax.xaxis.grid(which="minor", color='k', linestyle='-.', linewidth=0.7) plt.show()OutputRead More

Contour Hatching in Matplotlib Plot

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:53:39

798 Views

To plot contour with hatching, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x, y and z data points using numpy.Flat the x and y data points.Create a figure and a set of subplots.Plot a contour with different hatches.Create a colorbar for a scalar mappable instance.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-3, 5, 150).reshape(1, -1) y = np.linspace(-3, 5, 120).reshape(-1, 1) z = np.cos(x) + np.sin(y) x, y = ... Read More

Advertisements