Found 1034 Articles for Matplotlib

How do you directly overlay a scatter plot on top of a jpg image in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:59:04

2K+ Views

To directly overlay a scatter plot on top of a jpg image, we can take the following steps −Load an image "bird.jpg", using imread() method, Read an image from a file into an array.Now display data as an image.To plot scatter points on the image make lists for x_points and y_points.Generate random numbers for x and y and append in lists.Using scatter method, plot x and y points.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = plt.imread("logo2.jpg") im = plt.imshow(data) x_points = [] y_points ... Read More

Plot a histogram with Y-axis as percentage in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:57:06

2K+ Views

To plot a histogram with Y-axis as percentage in matplotlib, we can take the following steps −Create a list of numbers as y.Create a number of bins.Plot a histogram using hist() method, where y, bins, and edgecolor are passed in the argument.Store the patches to set the percentage on Y-axis.Create a list of colors from the given alphanumeric numbers.To set the percentage, iterate the patches (obtained in step 3).Set the Y-axis ticks range.To display the figure, use show() method.Exampleimport random import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True y = [4, 1, 8, ... Read More

Setting Y-axis in Matplotlib using Pandas

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:56:41

5K+ Views

To set Y-Axis in matplotlib using Pandas, we can take the following steps −Create a dictionary with the keys, x and y.Create a data frame using Pandas.Plot data points using Pandas plot, with ylim(0, 25) and xlim(0, 15).To display the figure, use show() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True d = dict(    x=np.linspace(0, 10, 10),    y=np.linspace(0, 10, 10)*2 ) df = pd.DataFrame(d) df.plot(kind="bar", ylim=(0, 25), xlim=(0, 15)) plt.show()Output

How to remove the outline of a circle marker when using pyplot.plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:52:23

885 Views

To remove the outline of a circle marker, we can reduce the value of marker edge width.Initialize list for x and y, with a single value.Limit x and y axis range for 0 to 5.Lay out a grid in current line style.Plot the given x and y using plot() method, with marker="o", markeredgecolor="red", markerfacecolor="green" and minimum markeredgewidth to remove the outline.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [4] y = [3] plt.xlim(0, 5) plt.ylim(0, 5) plt.grid() plt.plot(x, y, marker="o", markersize=20, markeredgecolor="black", markerfacecolor="green", markeredgewidth=.1) plt.show()OutputRead More

Python Matplotlib Venn diagram

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:50:18

2K+ Views

To plot a Venn diagram, first install Venn diagram using command "pip install matplotlib-venn". Using venn3, plot a 3-set area-weighted Venn diagram.StepsCreate 3 sets.Using venn3, make a Venn diagram.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from matplotlib_venn import venn3 plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True set1 = {'A', 'B', 'C'} set2 = {'A', 'B', 'D'} set3 = {'A', 'E', 'F'} venn3([set1, set2, set3], ('Group1', 'Group2', 'Group3')) plt.show()Output

How to improve the label placement for Matplotlib scatter chart?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:49:25

1K+ Views

To imporove the label placement for matplotlib scatter chart, we can first plot the scatter points and annotate those points with labels.StepsCreate points for x and y using numpy.Create labels using xpoints.Use scatter() method to scatter points.Iterate the labels, xpoints and ypoints and annotate the plot with label, x and y with different properties.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xpoints = np.linspace(1, 10, 10) ypoints = np.random.rand(10) labels = ["%.2f" % i for i in xpoints] plt.scatter(xpoints, ypoints, c=xpoints) for label, x, y in zip(labels, xpoints, ypoints): ... Read More

How to plot bar graphs with same X coordinates side by side in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:48:07

1K+ Views

To plot bar graphs with same X coordinates (G1, G2, G3, G4 and G5), side by side in matplotlib, we can take the following steps −Create the following lists – labels, men_means and women_means with different data elements.Return evenly spaced values within a given interval, using numpy.arrange() method.Set the width variable, i.e., width=0.35.Create fig and ax variables using subplots method, where default nrows and ncols are 1.The bars are positioned at *x* with the given *align*\ment. Their dimensions are given by *height* and *width*. The vertical baseline is *bottom* (default 0), so create rect1 and rect2 using plt.bar() method.Set the Y-axis label using plt.ylabel() ... Read More

Plot scatter points using plot method in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:47:24

486 Views

To plot scatter points using plot method in matplotlib, we can take the following steps−Create random data points (x1 and x2) using numpy.Plot x1 data points using plot() method with marker size 20 and green color.Plot x2 data points using plot() method with marker size 10 and red color.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x1 = np.random.randn(20) x2 = np.random.randn(20) plt.plot(x1, 'go', markersize=20) plt.plot(x2, 'ro', ms=10) plt.show()Output

Changing the formatting of a datetime axis in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:47:02

6K+ Views

To change the formatting of a datetime axis in matplotlib, we can take the following steps−Create a dataframe df using pandas DataFrame with time and speed as keysCreate a figure and a set of subplots using subplots() method.Plot the dataframe using plot method, with df's (Step 1) time and speed.To adjust the tick labels, we can rotate tick_params by 45 degreesTo edit the date formatting from %d-%m-%d to %d:%m%d, we can use set_major_formatter() method. Set the formatter of the major ticker.To display the figure, use show() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt, ... Read More

Save figure as file from iPython notebook using Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:46:34

4K+ Views

To save a figure as a file from iPython, we can take the following steps−Create a new figure or activate an existing figure.Add an axes to the figure using add_axes() method.Plot the given list.Save the plot using savefig() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_axes([1, 1, 1, 1]) plt.plot([1, 2]) plt.savefig('test.png', bbox_inches='tight')OutputWhen we execute the code, it will save the following plot as "test.png".

Advertisements