Found 1034 Articles for Matplotlib

How to read an input image and print it into an array in matplotlib?

Rishikesh Kumar Rishi
Updated on 11-Oct-2021 08:27:39

1K+ Views

To read an input image and print it into an array in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Read an image from a file into an array. Use plt.imread() method.Print the Numpy array of the image.To turn off the axis, use axis('off') 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 im = plt.imread("forest.jpg") print("Numpy array of the image is: ", im) im = plt.imshow(im) plt.axis('off') plt.show()OutputIt will produce the following output −On the ... Read More

How to create minor ticks for a polar plot in matplotlib?

Rishikesh Kumar Rishi
Updated on 11-Oct-2021 08:22:01

945 Views

To create minor ticks for a polar plot in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create r (radius) and theta data points using numpy.Add a subplot to the current figure.Iterate the points between 0 to 360 with step=10 and plot them to get the ticks.To display the figure, use Show() method.Exampleimport numpy as np import matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # radius and theta for the polar plot r = np.arange(0, 5, 0.1) theta = 2 ... Read More

How to plot an animated image matrix in matplotlib?

Rishikesh Kumar Rishi
Updated on 11-Oct-2021 08:20:14

2K+ Views

To plot an animated image matrix in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make an animation by repeatedly calling a function *update*.Inside the update method, create a 6×6 dimension of matrix and display the data as an image, i.e., on a 2D regular raster.Turn off the axes using set_axis_off().To display the figure, use Show() method.Examplefrom matplotlib.animation import FuncAnimation import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() def ... Read More

How to put xtick labels in a box matplotlib?

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:29:23

448 Views

To put xtick labels in a box, we can take the following stepsStepsCreate a new figure or activate an existing figure.Get the current axis of the figure.Set the left and bottom position of the axes.Set the position of the spines, i.e., bottom and left.To put xtick labels in a box, iterate the ticklabels and use set_bbox() method.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 plt.figure() ax = plt.gca() ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.spines['bottom'].set_position(('data', 0)) ax.spines['left'].set_position(('data', 0)) for label in ax.get_xticklabels():    label.set_fontsize(12)    label.set_bbox(dict(facecolor='red', edgecolor='black', alpha=0.7)) ... Read More

How to plot a time as an index value in a Pandas dataframe in Matplotlib?

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:28:32

2K+ Views

To plot a time as an index value in a Pandas dataframe in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with two columns, time and speed.Set the DataFrame index using existing columns.To display the figure, use Show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Pandas dataframe df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10))) # Set the dataframe index df.set_index('time').plot() # ... Read More

How to put a title for a curved line in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-Oct-2021 08:14:36

469 Views

To put a title for a curved line in Python Matplotlib, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points such that the line would be a curve.Plot the x and y data points.Place a title for the curve plot using plt.title() method.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create x and y data points x = np.linspace(-1, 1, 50) y = 2**x + 1 # Plot ... Read More

Matplotlib – How to plot the FFT of signal with correct frequencies on the X-axis?

Rishikesh Kumar Rishi
Updated on 11-Oct-2021 08:12:10

2K+ Views

To plot the FFT (Fast Fourier Transform) of a signal with correct frequencies on the X-axis in matplotlib, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Initialize two variables, N and m, to calculate nu.Create the signal (a sine wave) using numpy. Compute the one-dimensional discrete Fourier Transform.Return the Discrete Fourier Transform sample frequencies.Plot the freq and fourier transform data points.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 t = np.arange(N) m ... Read More

When is plt.Show() required to show a plot and when is it not?

Rishikesh Kumar Rishi
Updated on 09-Oct-2021 09:44:21

2K+ Views

plt.Show() would help whenever there is no interactive plot.fig.Show() would help to display all the figures if it is interactive.Let's take an example to observe the difference between plt.Show() and fig.Show().StepsOpen iPython shell.Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Plot a line using plot() method.Display the figure using Show() method.To display the figure, use Show() method with block=False.Exampleimport numpy as np from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create a new figure fig ... Read More

How to remove the axis tick marks on a Seaborn heatmap?

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:26:36

5K+ Views

To remove the axis tick marks on a Seaborn heatmap, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create random data points with 4×4 dimension.Plot the rectangular data as a color-encoded matrix.Use tick_params() for changing the appearance of ticks and tick labels. Use left=false and bottom=false to remove the tick marks.To display the figure, use Show() method.Exampleimport numpy as np import seaborn as sns 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) ax = sns.heatmap(data, vmax=1) ax.tick_params(left=False, bottom=False) ... Read More

Make logically shading region for a curve in matplotlib

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:25:07

146 Views

To make logically shading region for a curve in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create t, s1 and s2 data points using numpy.Create a figure and a set of subplots.Plot t and s1 data points; add a horizontal line across the axis.Create a collection of horizontal bars spanning *yrange* with a sequence of xranges.Add a '~.Collection' to the axes' collections; return the collection.To display the figure, use Show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.collections as collections plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ... Read More

Advertisements