Found 1034 Articles for Matplotlib

How to produce a barcode in Matplotlib?

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:31:51

258 Views

To produce a barcode in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a list of binary numbers, i.e., 0s and 1s.Create a new figure or activate an existing figure with dpi=100Add an axes to the figure.Turn off the axes.Use imshow() method to plot the data from step 2.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 code = np.array([ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, ... Read More

How to set label for an already plotted line in Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Jun-2021 12:09:33

1K+ Views

To set label for an already plotted line in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Plot the line with an input list.Set the label of the created line.Place a legend on the plot at the "upper right" location.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 line, = plt.plot([2, -1, 4, -1, 2]) line.set_label("line") plt.legend(loc="upper right") plt.show()Output

How to handle times with a time zone in Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Jun-2021 12:09:06

392 Views

To handle times with a time zone in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data.To handle times with a time zone, use pytz library that brings the Olson tz database into Python. This library allows accurate and cross-platform timezone calculations.Plot the dataframe using plot() method.To display the figure, use show() method.Exampleimport pandas as pd import numpy as np from matplotlib import pyplot as plt import pytz plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame( ... Read More

How to change the default path for "save the figure" in Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Jun-2021 12:06:03

4K+ Views

To change the default path for "save the figure", we can use rcParams["savefig.directory"] to set the directory path.StepsSet the figure size and adjust the padding between and around the subplots.Create random data using numpy.Use imshow() method. Display the data as an image, i.e., on a 2D regular raster.Save the figure using plt.savefig() method.Exampleimport os import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True dir_name = "C:/Windows/Temp/" plt.rcParams["savefig.directory"] = os.chdir(os.path.dirname(dir_name)) data = np.random.rand(5, 5) plt.imshow(data, cmap="copper") plt.savefig("img.png")OutputWhen we execute the code, it will save the following plot as ... Read More

How to have actual values in Matplotlib Pie Chart displayed?

Rishikesh Kumar Rishi
Updated on 17-Jun-2021 12:05:32

6K+ Views

To have actual or any custom values in Matplotlib pie chart displayed, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make lists of labels, fractions, explode position and get the sum of fractions to calculate the percentageMake a pie chart using labels, fracs and explode with autopct=lambda p: .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 = ('Read', 'Eat', 'Sleep', 'Repeat') fracs = [5, 3, 4, 1] total = sum(fracs) explode = (0, 0.05, 0, 0) ... Read More

How to plot MFCC in Python using Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Jun-2021 12:04:39

1K+ Views

To plot MFCC in Python, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Open and read a WAV file.Compute MFCC features from an audio signal.Create a figure and a set of subplots.Interchange two axes of an arrayDisplay the data as an image, i.e., on a 2D regular raster.To display the figure, use show() method.Examplefrom python_speech_features import mfcc import scipy.io.wavfile as wav import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True (rate, sig) = wav.read("my_audio.wav") mfcc_data = mfcc(sig, rate) fig, ax = plt.subplots() ... Read More

How can I convert from scatter size to data coordinates in Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Jun-2021 12:03:56

262 Views

To convert from scatter size to data coordinates in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and s data points using numpy.Create a figure and a set of subplots.Make a scatter plot with X and s, cmap and color info.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 X = np.array([[1, 1], [2, 1], [2.5, 1]]) s = np.array([20, 10000, 10000]) fig, ax = plt.subplots() ax.scatter(X[:, 0], X[:, ... Read More

How to load a .ttf file in Matplotlib using mpl.rcParams?

Rishikesh Kumar Rishi
Updated on 17-Jun-2021 12:02:13

355 Views

To load a .ttf file in Matplotlib using mpl.rcParams, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize the path for the .ttf file.Get an instance of a class for storing and manipulating the font properties.Set the font family with the name of the font that best matches the font properties.Create a figure and a set of subplots.Set the title of the figure.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, font_manager as fm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True path = '/usr/share/fonts/truetype/malayalam/Karumbi.ttf' ... Read More

Scroll backwards and forwards through Matplotlib plots

Rishikesh Kumar Rishi
Updated on 17-Jun-2021 12:01:51

1K+ Views

To scroll backward and forwards (left and right keys) through Matplotlib plots, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create curr_pos and y using numpy.Create a new figure or activate an existing figure using figure() method.Bind the function to the event, i.e., key_press_event.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Plot curr_pos and y data points using plot() method.If the left and right arrow keys could be used, then the curve could go right and left accordingly.To display the figure, use show() method.Exampleimport numpy as np ... Read More

How to increase the thickness of error line in a Matplotlib bar chart?

Rishikesh Kumar Rishi
Updated on 17-Jun-2021 12:00:56

1K+ Views

To increase the thickness of error line in a Matplotlib bar chart, we can use err_kw=dict() with their properties.StepsSet the figure size and adjust the padding between and around the subplots.Make a dictionary of bar details.Create a figure and a set of subplots.Use bar() method to make a bar plot with yerr and err_kwTo display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True bar_details = {    "labels": ['G1', 'G2', 'G3', 'G4', 'G5'],    "men_means": [20, 35, 30, 35, 27],    "men_std": [2, 3, 4, 1, 2],    "width": 0.35 } ... Read More

Advertisements