Found 784 Articles for Data Visualization

How to animate a pcolormesh in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:05:57

2K+ Views

To animate pcolormesh in matplotlib, we can take the following steps −Create a figure and a set of subplots.Create x, y and t data points using numpy.Create X3, Y3 and T3, return coordinate matrices from coordinate vectors using meshgrid.Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.Make a colorbar with colormesh axis.Animate pcolormesh using Animation() class method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, animation plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.linspace(-3, 3, 91) t = np.linspace(0, 25, 30) y = np.linspace(-3, 3, 91) X3, Y3, T3 = ... Read More

How to add a colorbar for a hist2d plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:01:58

3K+ Views

To add a colorbar for hist2d plot, we can pass a scalar mappable object to colorbar() method's argument.StepsCreate x and y data points using numpy.Create a figure and a set of subplots using subplots() method.Make a 2D histogram plot using hist2d() method.Create a colorbar for a hist2d scalar mappable instance.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, colors plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.randn(100) y = np.random.randn(100) + 5 fig, ax = plt.subplots() hh = ax.hist2d(x, y, bins=40, norm=colors.LogNorm()) fig.colorbar(hh[3], ax=ax) plt.show()OutputRead More

How to plot a density map in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 29-Sep-2021 12:38:42

3K+ Views

To plot a density map in Python, we can take the following steps −Create side, x, y, and z using numpy. Numpy linspace helps to create data between two points based on a third number.Return coordinate matrices from coordinate vectors using side data.Create exponential data using x and y (Step 2).Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, cm, colors import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True side = np.linspace(-2, 2, 15) X, Y = np.meshgrid(side, side) Z = np.exp(-((X - 1) ... Read More

How to write text in subscript in the axis labels and the legend using Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Oct-2023 21:14:44

28K+ Views

To write text in subscript in the axis labels and the legend, we can take the following steps −Create x and y data points using NumPy.Plot x and y data points with a super subscript texts label.Use xlabel and ylabel with subscripts in the text.Use the legend() method to place a legend in the plot.Adjust the padding between and around subplots.To display the figure, use the show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 1000) y = np.exp(x) plt.plot(x, y, label=r'$e^x$', c="red", lw=2) plt.xlabel("$X_{axis}$") plt.ylabel("$Y_{axis}$") plt.legend(loc='upper left') plt.show()OutputRead More

How to set axis ticks in multiples of pi in Python Matplotlib?

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

3K+ Views

To set axis ticks in multiples of pi in Python, we take following steps −Initialize a pi variable, create theta and y data points using numpy.Plot theta and y using plot() method.Get or set the current tick locations and labels of the X-axis using xticks() method.Convenience method to set or retrieve autoscaling margins using margins() method.To 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 pi = np.pi theta = np.arange(-2 * pi, 2 * pi+pi/2, step=(pi / 2)) y = np.sin(theta) plt.plot(theta, y) plt.xticks(theta, ['-2π', '-3π/2', 'π', ... Read More

How to make hollow square marks with Matplotlib in Python?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:40:16

4K+ Views

To make hollow square marks with matplotlib, we can use marker 'ks', markerfacecolor='none', markersize=15, and markeredgecolor=red.StepsCreat x and y data points using numpy.Create a figure or activate an existing figure, add an axes to the figure as part of a subplot arrangement.Plot x and y data points using plot() method. To make hollow square marks, we can use marker "ks" and markerfacecolor="none", markersize="15" and markeredge color="red".To 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 x = np.linspace(-2, 2, 10) y = np.sin(x) fig = plt.figure() ax1 = ... Read More

How to display all label values in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:37:08

5K+ Views

To display all label values, we can use set_xticklabels() and set_yticklabels() methods.StepsCreate a list of numbers (x) that can be used to tick the axes.Get the axis using subplot() that helps to add a subplot to the current figure.Set the ticks on X and Y axes using set_xticks and set_yticks methods respectively and list x (from step 1).Set tick labels with label lists (["one", "two", "three", "four"]) and rotation of 45 using set_xticklabels() and set_yticklabels().To add space between axes and tick labels, we can use tick_params() method with pad argument that helps to add space. Argument direction (in) helps to put ticks inside ... Read More

How can I place a table on a plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:31:31

669 Views

StepsUsing the subplots() method, create a figure and a set of subplots with figure size (7, 7).Create a data frame with two keys, time and speed.Get the size of the array.Add a table to the current axis using the table method.Shrink the font size until the text fits into the cell width.Set the font size in the table.Set the face color, edge color,  and text color by iterating the matplotlib table.Save and display the figure.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), ... Read More

How to convert a .wav file to a spectrogram in Python3?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:25:40

2K+ Views

To convert a .wav file to a spectrogram in python3, we can take the following steps −Load a .wav file from local machine.Compute a spectrogram with consecutive Fourier transforms using spectrogram() method.Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.Use imshow() method with spectrogram.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from scipy import signal from scipy.io import wavfile plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True sample_rate, samples = wavfile.read('test.wav') frequencies, times, spectrogram = signal.spectrogram(samples, sample_rate) plt.pcolormesh(times, frequencies, spectrogram, shading='flat') plt.imshow(spectrogram) plt.show()OutputRead More

Draw axis lines or the origin for Matplotlib contour plot.

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:22:57

4K+ Views

To draw axis lines or the origin for matplotlib contour plot, we can use contourf(),  axhline() y=0 and axvline() x=0.Create data points for x, y, and z using numpy.To set the axes properties, we can use plt.axis('off') method.Use contourf() method with x, y, and z data points.Plot x=0 and y=0 lines with red color.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 x = np.linspace(-1.0, 1.0, 10) x, y = np.meshgrid(x, x) z = -np.hypot(x, y) plt.axis('off') plt.contourf(x, y, z, 10) plt.axhline(0, color='red') plt.axvline(0, color='red') plt.show()OutputRead More

Advertisements