Add Legend to a Matplotlib Pie Chart

Rishikesh Kumar Rishi
Updated on 16-Jun-2021 12:22:53

8K+ Views

To add a legend to a Matplotlib pie chart, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a list of labels, colors, and sizes.Use pie() method to get patches and texts with colors and sizes.Place a legend on the plot with patches and labels.Set equal scaling (i.e., make circles circular) by changing the axis limits.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 = ['Walk', 'Talk', 'Sleep', 'Work'] sizes = [23, 45, 12, 20] colors = ['red', 'blue', ... Read More

Make More Than 10 Subplots in a Figure Using Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Jun-2021 12:21:16

1K+ Views

To make more than 10 subplots in a figure, we can use subplots() method with some rows and columns.StepsSet the figure size and adjust the padding between and around the subplots.Initialize rows count and columns count.Create a figure and a set of subplots with rows☓cols subplots.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True rows = 4 cols = 3 fig, axes = plt.subplots(nrows=rows, ncols=cols) plt.show()Output

Plot Jointplot with Hue Parameter in Seaborn

Rishikesh Kumar Rishi
Updated on 16-Jun-2021 12:20:58

413 Views

To plot a jointplot with hue parameter in Seaborn, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Create x data points using numpy.Make a dictionary with some curve data.Make a dataframe for tabular data.Make a jointplot using jointplot() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd import seaborn as sns import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 1, 5) d = {       'y=sin(x)': np.sin(x),       'y=cos(x)': np.cos(x), ... Read More

Annotate the End of Lines Using Python and Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Jun-2021 12:20:16

1K+ Views

To annotate the end of lines using Python and Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initalize a variable, rows, to get the number of rows data.Get a Pandas dataframe in a rectangular tabular data.Calculate the cumsum (cumulative sum) of the dataframe.Plot the dataframe using plot() method.Iterate line and name to annotate the end of lines.Use annotate() method with column's name, xy co-ordinates, color of the lines, sizes, etc.Place a legend on the figure.To display the figure, use show() method.Exampleimport pandas as pd import numpy as np import ... Read More

Define Multiple Plots to be Animated with a For Loop in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Jun-2021 12:19:49

3K+ Views

To define multiple plots to be animated with a for loop in matplotlib, we can take followings steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure method.Add an axes to the current figure and make it the current axes.Initialize two variables, N and x, using numpy.Get the list of lines and bar patches.Animate the lines and rectangles (bar patches) in a for loop.Make an animation by repeatedly calling a function *func*.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt from matplotlib import ... Read More

Return Matplotlib Figure Object from Pandas Plot Function

Rishikesh Kumar Rishi
Updated on 16-Jun-2021 12:19:21

762 Views

To return a matplotlib.figure.Figure object from Pandas function, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe, df.Make a horizontal bar plot using barh() method.Get the current figure instance.Place a legend on the axes at the lower-right location.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': range(10)}) ax = df.plot.barh(color=(1, 0, 0, 0.25)) fig = ax.get_figure() ax.legend(loc='lower right') plt.show()OutputRead More

Plot Half Black and Half White Circle Using Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Jun-2021 12:19:01

2K+ Views

To plot a half-black and half-white circle using Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Initialize theta1 and theta2 to draw edges from theta1 to theta2 and vice-versa.Add the wedge instance on the current axes.Set equal scaling by changing axis limits.Set x and y scale.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from matplotlib.patches import Wedge plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() theta1, theta2 = 0, 0 + 180 radius = 2 ... Read More

Construct a TM for Adding 1 to a Binary Natural Number

Bhanu Priya
Updated on 16-Jun-2021 12:18:10

1K+ Views

A Turing machine (TM) can be formally described as seven tuples −(Q, X, ∑, δ, q0, B, F)Where, Q is a finite set of states.X is the tape alphabet.∑ is the input alphabet.δ is a transition function: δ:QxX->QxXx{left shift, right shift}.q0 is the initial state.B is the blank symbol.F is the final state.Binary numbers1 = 12 = 103 = 114 = 1005 = 1016 = 110. . .AlgorithmStep 1 − Move to the right end of the string.Step 2 − Repeat:If the current cell contains 1, write 0 and move left until the current cell contains 0 or blank.Step 3 ... Read More

Change Default Background Color for Matplotlib Plots

Rishikesh Kumar Rishi
Updated on 16-Jun-2021 12:17:07

648 Views

To change the default background color for Matplotlib plots, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Get the current axis.Add a subplot to the current figure, with nrows=1, ncols=2 and index=1.Plot random x and y data points using plots() method.Set the title of the subplot.Add a subplot to the current figure with nrows=1, ncols=2 and index=2.Get the current axis.Set the customize face color.Plot x and y data points using plot() method.Set the title of the plot.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt ... Read More

Add Shared X and Y Labels to Pandas Plot with Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Jun-2021 12:16:44

840 Views

To add a shared x-label and shared y-label, we can use plot() method with kind="bar", sharex=True and sharey=True.StepsSet the figure size and adjust the padding between and around the subplots.Create a two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot the dataframe with kind="bar", sharex=True and sharey=True.To display the figure, use show() method.Exampleimport pandas as pd import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(    {'First': [0.3, 0.2, 0.5, 0.2], 'Second': [0.1, 0.0, 0.3, 0.1],    'Third': [0.2, 0.5, 0.0, 0.7], 'Fourth': [0.6, 0.3, 0.4, 0.6]},    index=list('1234')) axes = df.plot(kind="bar", subplots=True, layout=(2, 2), ... Read More

Advertisements