Found 10476 Articles for Python

How do you just show the text label in a plot legend in Matplotlib?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:34:52

1K+ Views

To just show the text label in plot legend we can use legend method with handlelength=0, handletextpad=0 and fancybox=0 in the arguments.StepsSet the figure size and adjust the padding between and around the subplots.Create random x and y data points using numpy.Create a figure and a set of subplots using subplots() method.Plot x and y data points using plot() method with label "Zig-Zag" for the legend.Use legend() method to place the label for the plot with handlelength=0, handletextpad=0 and fancybox=0 in the arguments.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = ... Read More

Box plot with min, max, average and standard deviation in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:34:20

6K+ Views

To make a box plot for min, max, average and standard deviation in matplotlib, StepsSet the figure size and adjust the padding between and around the subplots.Create a random dataset of 5☓5 dimension.Find min, max, average and standard deviation from the data.Make a Pandas dataframe with Step 3, min, max, average and standard deviation data.Make a box plot from the dataframe column.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randn(5, 5) min = data.min(0) max = data.max(0) avg = data.mean(0) std = data.std(0) df = ... Read More

Adding a scatter of points to a boxplot using Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:33:17

5K+ Views

To add a scatter of points to a boxplot using matplotlib, we can use boxplot() method and enumerate the Pandas dataframe to get the x and y data points to plot the scatter points.StepsSet the figure size and adjust the padding between and around the subplots.Make a dataframe using DataFrame class with the keys, Box1 and Box2.Make boxplots from the dataframe.Find x and y for the scatter plot using data (Step 1).To display the figure, use show() method.Exampleimport pandas as pd import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = ... Read More

How to center labels in a Matplotlib histogram plot?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:33:02

7K+ Views

To place the labels at the center in a histogram plot, we can calculate the mid-point of each patch and place the ticklabels accordinly using xticks() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a random standard sample data, x.Initialize a variable for number of bins.Use hist() method to make a histogram plot.Calculate the list of ticks at the center of each patch.Make a list of tickslabels.Use xticks() method to place xticks and labels.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = ... Read More

Show tick labels when sharing an axis in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:31:06

895 Views

To show the tick labels when sharing an axis, we can just use the subplot() method with sharey argument. By default, y ticklabels could be visible.StepsSet the figure size and adjust the padding between and around the subplots.Add a subplot to the current figure using subplot() method, where nrows=1, ncols=2 and index=1 for axis ax1.Plot a line on the axis 1.Add a subplot to the current figure, using subplot() method, where nrows=1, ncols=2 and index=2 for axis ax2.Plot a line on the axis 2.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"] ... Read More

How to extract data from a Matplotlib plot?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:32:47

15K+ Views

To extract data from a plot in matplotlib, we can use get_xdata() and get_ydata() methods.StepsSet the figure size and adjust the padding between and around the subplots.Create y data points using numpy.Plot y data points with color=red and linewidth=5.Print a statment for data extraction.Use get_xdata() and get_ydata() methods to extract the data from the plot (step 3).Print x and y data (Step 5).To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True y = np.array([1, 3, 2, 5, 2, 3, 1]) curve, = plt.plot(y, c='red', lw=5) print("Extracting ... Read More

How to plot complex numbers (Argand Diagram) using Matplotlib?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:22:41

649 Views

To plot complex numbers using matplotlib, we can make a dataset with complex numbers.StepsSet the figure size and adjust the padding between and around the subplots.Create random complex numbers.Create a figure and a set of subplots using subplots() method.Plot the scatter points using scatter() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(10) + 1j*np.random.rand(10) fig, ax = plt.subplots() ax.scatter(data.real, data.imag, c=data.real, cmap="RdYlBu_r") plt.show()OutputRead More

Plotting multiple line graphs using Pandas and Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:22:03

4K+ Views

To plot multiple line graphs using Pandas and Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a 2D potentially heterogeneous tabular data using Pandas DataFrame class, where the column are x, y and equation.Get the reshaped dataframe organized by the given index such as x, equation, and y.Use the plot() method to plot the lines.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame([    ["y=x^3", 0, 0],    ["y=x^3", 1, 1], ... Read More

Program to find out the lowest common ancestor of a binary tree of given nodes using Python

Arnab Chakraborty
Updated on 29-May-2021 13:15:40

183 Views

Suppose, we are given a binary tree and are asked to find out the lowest common ancestor of all the nodes in the tree. The lowest common ancestor in a binary tree is the lowest node of which the nodes x1, x2, x3, ...., xn are descendants. A particular node can also be a descendant of itself. We have to find the node and return it as an output. The inputs are the root node of the tree and the list of nodes that we have to find the ancestor of.So, if the input is likeand the list of nodes ... Read More

Program to change the root of a binary tree using Python

Arnab Chakraborty
Updated on 29-May-2021 13:24:48

904 Views

Suppose, we are given a binary tree and a node that is situated at the leaf of the binary tree.We have to make the leaf node the root node of the binary tree. We can do it in the following way −If a node has a left child, it becomes the right child.A node's parent becomes its left child. In this process, the parent node's link to that node becomes null, so it will have only one child.The node structure of the tree is like below −TreeNode:    data:    left:    right:    parent: We have ... Read More

Advertisements