Python Articles

Page 670 of 852

How to modify a Matplotlib legend after it has been created?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 18-Jun-2021 2K+ Views

To modify a Matplotlib legend after it has been created, we can have multiple methods to modify the created legend.Set the figure size and adjust the padding between and around the subplots.Plot a line using plot() method, with two lists and a label.Use legend() method to place a legend over the plot.To modify the matplotlib legend, use set_title() method.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 plt.plot([1, 3, 4, 5, 2, 1], [3, 4, 1, 3, 0, 1],          label="line plot", color='red', lw=0.5) ...

Read More

Plotting error bars from a dataframe using Seaborn FacetGrid (Matplotlib)

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 18-Jun-2021 618 Views

To plot error bars from a dataframe using Seaborn FacetGrid, we can use following steps −Get a two-dimensional, size-mutable, potentially heterogeneous tabular data.Multi-plot grid for plotting conditional relationships.Apply a plotting function to each facet's subset of the data.To display the figure, use show() method.Exampleimport pandas as pd import seaborn as sns from matplotlib import pyplot as plt df = pd.DataFrame({'col1': [3.0, 7.0, 8.0],                   'col2': [1.0, 4.0, 3.0]}) g = sns.FacetGrid(df, col="col1", hue="col1") g.map(plt.errorbar, "col1", "col2", yerr=0.75, fmt='o') plt.show()Output

Read More

How can I get pyplot images to show on a console app? (Matplotlib)

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 18-Jun-2021 2K+ Views

To show pyplot images on a console, we can use pyplot.show() method.StepsSet the figure size and adjust the padding between and around the subplots.Create random data of 5☓5 dimension.Use imshow() method, with data. Display the data as an image, i.e., on a 2D regular raster.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(5, 5) plt.imshow(data, cmap="copper") plt.show()Output

Read More

How to produce a barcode in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 18-Jun-2021 427 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 handle times with a time zone in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 17-Jun-2021 552 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
Rishikesh Kumar Rishi
Updated on 17-Jun-2021 5K+ 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
Rishikesh Kumar Rishi
Updated on 17-Jun-2021 7K+ 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
Rishikesh Kumar Rishi
Updated on 17-Jun-2021 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
Rishikesh Kumar Rishi
Updated on 17-Jun-2021 354 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
Rishikesh Kumar Rishi
Updated on 17-Jun-2021 524 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
Showing 6691–6700 of 8,519 articles
« Prev 1 668 669 670 671 672 852 Next »
Advertisements