Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Rishikesh Kumar Rishi
Page 61 of 102
How to animate a sine curve in Matplotlib?
To make animated sine curve, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an axes to the current figure and make it the current axes.Plot a line with empty lists.To initialize the line, pass empty lists.To animate the sine curve, update sine curve values and return the line instance.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt from matplotlib import animation plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax ...
Read MoreMatplotlib histogram with multiple legend entries
To plot a histogram with multiple legend entries, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create random data using numpyPlot a histogram using hist() method.Make a list of colors to color the face of each patch.Iterate the patches and set face color of each patch.Create a list of handles to place the legend.Use legend() method for multiple legend entries.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np from matplotlib.patches import Rectangle plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rayleigh(size=1000) * ...
Read MoreDifferentiate the orthographic and perspective projection in Matplotlib
To plot perspective and orthographic projection plots, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Set the projection type as 'perspective' on ax1 axis.Set the title of the plot.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Set the projection type as 'orthographic' on ax2 axis.Set the title of the plot.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 fig ...
Read MoreHow to create a Matplotlib bar chart with a threshold line?
To create a Matplotlib bar chart with a threshold line, we have to use axhline() method.StepsSet the figure size and adjust the padding between and around the subplots.Initialize a variable, threshold.Make lists for bars values.Get the below and above bar values based on the threshold value.Create a figure and a set of subplots using subplots() method.Plot bars with x, a_threshold and b_threshold values.Add a horizontal line across the axis using axhline() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True threshold = 10 values = np.array([8.0, 10.0, ...
Read MoreHow to plot a remote image from http url using Matplotlib?
To plot a remote image from an http URL, we can use io.imread() method to read an URL and take the following steps −Set the figure size and adjust the padding between and around the subplots.Load an image from an http URLUse imshow() method to display data as an image, i.e., on a 2D regular raster.Turn off the axes.To display the figure, use show() method.Examplefrom skimage import io import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True f = "http://matplotlib.sourceforge.net/_static/logo2.png" a = io.imread(f) plt.imshow(a) plt.axis('off') plt.show()Output
Read MoreHow to add text inside a plot in Matplotlib?
To add text inside a plot in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Place text with some text properties.Plot x and y using plot() method.Turn off the axes.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.linspace(-5, 5, 100) y = x ** 3 plt.text(0, 100, '$y=x^3$', fontsize=22, bbox=dict(facecolor='red', alpha=0.5)) plt.plot(x, y, c='g') plt.axis('off') plt.show()Output
Read MoreHow to place X-axis grid over a spectrogram in Python Matplotlib?
To place X-axis grid over a spectrogram in Python, we can use grid() method and take the following steps −Set the figure size and adjust the padding between and around the subplots.Create t, s1, s2, nse, x, NEFT and Fs data points using numpy.Create a new figure or activate an existing figure using subplots() method with nrows=2.Plot t and x data points using plot() method.Lay out a grid in current line style.Set X-axis margins.Plot a spectrogram using specgram() method.Lay out a grid in current line style with dotted linestyle and some other properties.To display the figure, use show() method.Exampleimport matplotlib.pyplot ...
Read MoreHow to hide axes but keep axis-labels in 3D Plot with Matplotlib?
To hide axes but keep axis-labels in 3D plot with Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Create x, y, z, dx, dy and dz data points using numpyUse bar3d() method to plot the 3D bars.To hide the axes, initialize a color tuple, the same as axes color.Set x, y and z axes plane color property same with the color tuple.Set x, y and z axes line color property ...
Read MoreHow to plot a rectangle on a datetime axis using Matplotlib?
To plot a recatangle on a datetime axis using matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.To define a rectangle, find the anchor points using datetime and matplotlib's dates.Add a '~.Patch' to the axes' using add_patch() method.Set major axis locator and formatter.Limit x and y axes scale.To display the figure, use show() method.Examplefrom datetime import datetime, timedelta from matplotlib.patches import Rectangle import matplotlib.pyplot as plt ...
Read MoreHow to remove scientific notation from a Matplotlib log-log plot?
To remove scientific notation from a matplotlib log-log plot, we can use ax.xaxis.set_minor_formatter(mticker.ScalarFormatter()) statement.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points using scatter() method.Set x and y axes sacle using set_xscale() and set_yscale() methods.To remove scientific notation, use format tick values as a number.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, ticker as mticker plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([1, 7, 6, 4, 0]) y = np.array([6, 2, 3, ...
Read More