Found 1034 Articles for Matplotlib

Annotate data points while plotting from Pandas DataFrame

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:11:45

3K+ Views

To annotate data points while plotting from pandas data frame, we can take the following steps −Create df using DataFrame with x, y and index keys.Create a figure and a set of subplots using subplots() method.Plot a series of data frame using plot() method, kind='scatter', ax=ax, c='red' and marker='x'.To annotate the scatter point with the index value, iterate the data frame.To display the figure, use show() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt import string plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'x': np.random.rand(10), 'y': np.random.rand(10)}, index=list(string.ascii_lowercase[:10])) fig, ax = plt.subplots() df.plot('x', ... Read More

How to fill color below a curve in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:10:56

9K+ Views

To fill color below a curve, we can take the following steps −StepsInitialize variable n. Initialize x and y data points using numpy.Create a figure and a set of subplots, fig and ax.Plot the curve using plot() method.Use fill_between() method to fill the area between the two curves, with -1 value.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True n = 256 X = np.linspace(-np.pi, np.pi, n, endpoint=True) Y = np.sin(2 * X) fig, ax = plt.subplots() ax.plot(X, Y, color='blue', alpha=1.00) ax.fill_between(X, Y, 0, color='blue', alpha=.1) plt.show()OutputRead More

Plotting a histogram from pre-counted data in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:09:01

2K+ Views

To plot a histogram from pre-counted data in matplotlib, we can take the following steps −Create a list of numbers.Make a pre-counted list with the help of input data.Plot a histogram with data, color=red, and label=data, using hist() method.Plot another histogram with counted data, color=default, and label=counted_data, using hist() method.To place the legend, use legend() method.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 data = [1, 2, 2, 3, 4, 5, 5, 5, 5, 6, 10] counted_data = {1: 1, 2: 2, 3: 1, 4: 1, 5: 4, 6: 1, ... Read More

How to suppress Matplotlib warning?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:01:45

3K+ Views

Let's take an example. We create a set of data points such that it would generate some warnings. We will create data points x from −1 to 1 and try to find log in that range, which means it will throw an error at value 0, while calculating logs.StepsCreate data points for x and calculate log(x), using numpy.Plot x and y using plot() method.Use warnings.filterwarnings("ignore") to suppress the warning.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt import warnings plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True warnings.filterwarnings("ignore") x = np.linspace(-1, 1, 10) y ... Read More

How to insert a small image on the corner of a plot with Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:02:09

7K+ Views

To insert a small image on the corner of a plot with matplotlib, we can take the following steps−Read an image from a file into an array using imread() method.Using subplots() method, create a figure and add a set of subplots.Plot a line on the current axis.Create newax (new axis) to show the image array (Step 1).Turn off the newly created axis, created for an image insert.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True im = plt.imread('bird.jpg') # insert local path of the image. fig, ax = plt.subplots() ax.plot(range(10)) newax = ... Read More

Drawing a rectangle with only border in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:02:39

3K+ Views

To draw a rectangle with only border in matplotlib, we can take the following steps−Create a figure and a set of subplots.Get the current axes, creating one if necessary.Add a patch, i.e., a rectangle to the current axes that is returned in step 2. Set the facecolor attribute to 'none'.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True figure, _ = plt.subplots() ax = plt.gca() ax.add_patch(patches.Rectangle((.25, .25), .50, .50, edgecolor='orange', facecolor='none', linewidth=2)) plt.show()Output

How to change the color of the ticks in the colorbar in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:03:03

954 Views

To change the color of the ticks in the colorbar in matplotlib, we can take the following steps−Create a random 2D−Array using numpy, with 4☓4 dimension.Use imshow() method to display the data as an image.Create a colorbar using colorbar() method with scalar mappable instance of imshow().Use getp() method to return the value of an object's property or print all of them.Set the property of an artist object.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 = np.random.rand(4, 4) im = plt.imshow(data, cmap="twilight_shifted_r") cbar = plt.colorbar(im) ... Read More

How to show an Axes Subplot in Python?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:03:30

2K+ Views

To show an axes subplot in Python, we can use show() method. When multiple figures are created, then those images are displayed using show() method.StepsCreate x and y data points using numpy.Plot x and y using plot() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(10) y = np.exp(x) plt.plot(x, y) plt.show()Output

Plot a circle with an edgecolor and hatch in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:01:13

356 Views

To place edge color and hatch of a circle in matplotlib, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add a subplot method to the current axis.Create a circle instance using Circle() class with an edgecolor, hatch and linewidth of the edge.Add a circle path on the plot.To place the text in the circle, use text() method.Scale X and Y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) circle = matplotlib.patches.Circle((0, 0), radius=1, edgecolor="orange", ... Read More

How to fill color above the curve in Matplotlib Program?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:00:35

2K+ Views

To fill color above the curve, we can take the following steps −StepsInitialize the variable n. Initialize x and y data points using numpy.Create a figure and a set of subplots, fig and ax.Plot the curve using plot() method.Using fill_between() method, fill the area between two curves, with 1 value.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True n = 256 X = np.linspace(-np.pi, np.pi, n, endpoint=True) Y = np.sin(2 * X) fig, ax = plt.subplots() ax.plot(X, Y, color='blue', alpha=1.00) ax.fill_between(X, Y, 1, color='blue', alpha=.1) plt.show()OutputRead More

Advertisements