Found 728 Articles for Data Visualization

How to display all label values in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:37:08
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
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
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
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

How to plot a line graph from histogram data in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:19:44
To plot a line graph from histogram data in matplotlib, we use numpy histogram method to compute the histogram of a set of data.StepsAdd a subplot to the current figure, nrows=2, ncols=1 and index=1.Use numpy histogram method to get the histogram of a set of data.Plot the histogram using hist() method with edgecolor=black.At index 2, use the computed data (from numpy histogram). To plot them, we can use plot() 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 plt.subplot(211) data = np.array(np.random.rand(100)) y, binEdges = np.histogram(data, bins=100) plt.hist(data, bins=100, edgecolor='black') ... Read More

How to plot a multi-colored line, like a rainbow using Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:15:51
To plot multi-colored lines, like a rainbow, we can create a list of seven rainbow colors (VIBGYOR).StepsCreate x for data points using numpy.Create a list of colors (rainbow VIBGYOR).Iterate in the range of colors list length.Plot lines with x and y(x+i/20) using plot() method, with marker=o, linewidth=7 and colors[i] where i is the index.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(-1, 1, 10) colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] for i in range(len(colors)):    plt.plot(x, x+i/20, c=colors[i], lw=7, marker='o') plt.show()OutputRead More

How to remove the label on the left side in matplotlib.pyplot pie charts?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:11:13
To remove the label on the left side in a matplotlib pie chart, we can take the following steps −Create lists of hours, activities, and colors.Plot a pie chart using pie() method.To hide the label on the left side in matplotlib, we can use plt.ylabel("") with ablank string.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True hours = [8, 1, 11, 4] activities = ['sleeping', 'exercise', 'studying', 'working'] colors = ["grey", "green", "orange", "blue"] plt.pie(hours, labels=activities, colors=colors, autopct="%.2f") plt.ylabel("") plt.show()Output

How to animate a line plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 28-May-2021 15:16:11
To animate the line plot in matplotlib, we can take the following steps −Create a figure and a set of subplots using subplots() method.Limit x and y axes scale.Create x and t data points using numpy.Return coordinate matrices from coordinate vectors, X2 and T2.Plot a line with x and F data points using plot() method.To make animation plot, update y data.Make an animation by repeatedly calling a function *func*, current fig, animate, and interval.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 = ... Read More

How can I display text over columns in a bar chart in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 11:59:47
To display text over columns in a bar chart, we can use text() method so that we could place text at a specific location (x and y) of the bars column.StepsCreate lists for x, y and percentage.Make a bar plot using bar() method.Iterate zipped x, y and percentage to place text for the bars column.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 x = ['A', 'B', 'C', 'D', 'E'] y = [1, 3, 2, 0, 4] percentage = [10, 30, 20, 0, 40] ax = plt.bar(x, y) for x, y, p in zip(x, y, percentage): ... Read More

How to handle an asymptote/discontinuity with Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 11:48:28
To handle an asymptote/discontinuity with matplotlib, we can take the following steps −Create x and y data points using numpy.Turn off the axes plot.Plot the line with x and y data points.Add a horizontal line across the axis, x=0.Add a vertical line across the axis, y=0.Place legend for the curve y=1/x.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(-1, 1, 100) y = 1 / x plt.axis('off') plt.plot(x, y, label='y=1/x') plt.axhline(y=0, c='red') plt.axvline(x=0, c='red') plt.legend(loc='upper left') plt.show()OutputRead More
Advertisements