Found 10476 Articles for Python

How to handle times with a time zone in Matplotlib?

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

481 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

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
Updated on 17-Jun-2021 12:05:32

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
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

319 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

476 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

2K+ 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 do you get the current figure number in Python's Matplotlib?

Vikram Chiluka
Updated on 22-Sep-2022 13:03:20

3K+ Views

In this article, we are going to the current figure number in Python’s matplotlib. Matplotlib is a Python library that is a numerical-mathematical extension of the NumPy library. Pyplot is a state-based interface to a Matplotlib module that provides MATLAB-like functionality. Line Plot, Contour, Histogram, Scatter, 3D Plot, and other plots are available in Pyplot. Using plt.gcf().number What is plt.gcf() The matplotlib.pyplot.gcf() function is primarily used to obtain the current figure. One is generated using the figure() function if no current figure is available. matplotlib.pyplot.gcf() Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task ... 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

How to plot a time series array, with confidence intervals displayed in Python? (Matplotlib)

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

3K+ Views

To plot a time series array, with confidence intervals displayed in Python, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Get the time series array.Initialize a variable, n_steps, to get the mean and standard deviation.Get the under and above lines for confidence intervals.Plot the mean line using plot() method.Use fill_between() method to get the confidence interval.To display the figure, use show() method.Exampleimport numpy as np import pandas as pd import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True time_series_array = np.sin(np.linspace           ... Read More

Advertisements