Found 784 Articles for Data Visualization

Layering a contourf plot and surface_plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:09:22

132 Views

To layer a contourf plot and surface_plot in matplotlib, we can take the following Steps −Initialize the variables, delta, xrange, yrange, x and y using numpy.Create a new figure or activate an existing figure using figure() method.Get the current axis where projection='3d'.Create a 3d countour plot with x and y data points.Plot the surface with x and y data points.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True delta = 0.025 xrange = np.arange(-5.0, 20.0, delta) yrange = np.arange(-5.0, 20.0, delta) x, y = np.meshgrid(xrange, yrange) ... Read More

How to make a heatmap square in Seaborn FacetGrid using Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:08:59

508 Views

To make a heatmap square in Seaborn facetgrid, we cn use heatmap() method with 10×10 random data set.StepsCreate a random data of size 10×10, with minimum -1 and maximum 10.Plot rectangular data as a color-encoded matrix using heatmap() method with data and color map "twilight_r".To display the figure, use show() method.Exampleimport numpy as np import seaborn as sn import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randint(low=-1, high=10, size=(10, 10)) hm = sn.heatmap(data=data, cmap="twilight_r") plt.show()Output

Plotting points on the surface of a sphere in Python's Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:08:19

11K+ Views

To plot points on the surface of a sphere in Python, we can use plot_surface() method.StepsCreate a new figure or activate an existing figure using figure() method.Add a set of subplots using add_subplot() method with 3d projection.Initialize a variable, r.Get the theta value for spherical points and x, y, and z data points using numpy.Plot the surface using plot_surface() 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 fig = plt.figure() ax = fig.add_subplot(projection='3d') r = 0.05 u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j] x = np.cos(u) * ... Read More

Creating a 3D plot in Matplotlib from a 3D numpy array

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:02:05

16K+ Views

To create a 3D plot from a 3D numpy array, we can create a 3D array using numpy and extract the x, y, and z points.Create a new figure or activate an existing figure using figure() method.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.Create a random data of size=(3, 3, 3).Extract x, y, and z data from the 3D array.Plot 3D scattered points on the created axisTo display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax ... Read More

How to use matplotlib.animate to animate a contour plot in Python?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:01:38

514 Views

To animate a contour plot in matplotlib in Python, we can take the following steps−Create a random data of shape 10☓10 dimension.Create a figure and a set of subplots using subplots() method.Makes an animation by repeatedly calling a function *func* using FuncAnimation() class.To update the contour value in a function, we can define a method animate that can be used in FuncAnimation() class.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randn(800).reshape(10, 10, 8) fig, ax = plt.subplots() def animate(i): ax.clear() ax.contourf(data[:, ... Read More

How to position and align a Matplotlib figure legend?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:01:08

1K+ Views

To position and align a matplotlib figure legend, we can take the following steps−Plot line1 and line2 using plot() method.Place a legend on the figure. Use bbox_to_anchor to set the position and make horizontal alignment of the legend elements.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 line1, = plt.plot([1, 5, 1, 7], linewidth=0.7) line2, = plt.plot([5, 1, 7, 1], linewidth=2.0) plt.legend([line1, line2], ["line1", "line2"], bbox_to_anchor=(0.45, 1.0), ncol=2) plt.show()Output

How can I convert numbers to a color scale in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:00:44

2K+ Views

To convert numbers to a color scale in matplotlib, we can take the following steps.StepsCreate x, y and c data points using numpy.Convert the data points to Pandas dataframe.Create a new figure or activate an existing figure using subplots() method.Get the hot colormap.To linearly normalize the data, we can use Normalize() class.Plot the scatter points with x and y data points and linearly normalized colormap.Set the xticks for x data points.To make the colorbar, create a scalar mappable object.Use colorbar() method to make the colorbar.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, colors import numpy as ... Read More

Exporting an svg file from a Matplotlib figure

Rishikesh Kumar Rishi
Updated on 12-May-2021 12:22:14

10K+ Views

To export an SVG file from a matplotlib figure, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Create random x and y data points using numpy.Plot x and y data points using plot() method.Save the .svg format file using savefig() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.random.rand(10) y = np.random.rand(10) ax.plot(x, y, ls='dotted', linewidth=2, color='red') plt.savefig("myimg.svg")OutputWhen we execute this code, it will create an SVG file called "myimg.svg" and ... Read More

Plot numpy datetime64 with Matplotlib

Rishikesh Kumar Rishi
Updated on 12-May-2021 12:20:25

472 Views

To plot a time series in Python using matplotlib, we can take the following steps −Create x and y points using numpy.Plot the created x and y points using plot() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import datetime import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([datetime.datetime(2021, 1, 1, i, 0) for i in range(24)]) y = np.random.randint(100, size=x.shape) plt.plot(x, y) plt.show()Output

How to give sns.clustermap a precomputed distance matrix in Matplotlib?

Rishikesh Kumar Rishi
Updated on 12-May-2021 12:19:02

192 Views

To give sns.clustermap a dataset, we can take the following steps −Set multiple theme parameters in one step.Load an example dataset from the online repository (requires Internet).Return item and drop from the frame. Raise KeyError if not found, using pop() method.Plot a matrix dataset as a hierarchically-clustered heatmap using clustermap() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True sns.set_theme(color_codes=True) iris = sns.load_dataset("iris") species = iris.pop("species") g = sns.clustermap(iris) plt.show()OutputRead More

Advertisements