Found 784 Articles for Data Visualization

How to set X-axis values in Matplotlib Python?

Rishikesh Kumar Rishi
Updated on 22-Aug-2023 16:04:36

71K+ Views

To set X-axis values in matplotlib in Python, we can take the following steps −Create two lists for x and y data points.Get the xticks range value.Plot a line using plot() method with xtick range value and y data points.Replace xticks with X-axis value using xticks() 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 x = [45, 1, 34, 78, 100] y = [8, 10, 23, 78, 2] default_x_ticks = range(len(x)) plt.plot(default_x_ticks, y) plt.xticks(default_x_ticks, x) plt.show()Output

Animate a rotating 3D graph in Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:41:04

1K+ Views

To make a rotating 3D graph in matplotlib, we can use Animation class for calling a function repeatedly.StepsInitialize variables for number of mesh grids, frequency per second to call a function, frame numbers.Create x, y, and z array for a curve.Make a function to make z array using lambda function.To pass a function into the animation class, make a user-defined function to remove the previous plot and plot a surface using x, y, and zarray.Create a new figure or activate an existing figure.Add a subplot arrangement using subplots() method.Set the Z-axis limit using set_zlim() method.Call the animation class to animate the surface plot.To display ... Read More

How to plot two Seaborn lmplots side-by-side (Matplotlib)?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:35:10

9K+ Views

To plot two graphs side-by-side in Seaborn, we can take the following steps −To create two graphs, we can use nrows=1, ncols=2 with figure size (7, 7).Create a data frame with keys, col1 and col2, using Pandas.Use countplot() to show the counts of observations in each categorical bin using bars.Adjust the padding between and around the subplots.To display the figure, use show() method.Exampleimport pandas as pd import 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 f, axes = plt.subplots(1, 2) df = pd.DataFrame(dict(col1=np.linspace(1, 10, 5), col2=np.linspace(1, 10, 5))) sns.countplot(df.col1, x='col1', ... Read More

Show Matplotlib graphs to image as fullscreen

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:31:38

11K+ Views

To show matplotlib graphs as full screen, we can use full_screen_toggle() method.StepsCreate a figure or activate an existing figure using figure() method.Plot a line using two lists.Return the figure manager of the current figure.To toggle full screen image, use full_screen_toggle() 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 plt.figure() plt.plot([1, 2], [1, 2]) manager = plt.get_current_fig_manager() manager.full_screen_toggle() plt.show()Output

How to make a 4D plot with Matplotlib using arbitrary data?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:28:35

5K+ Views

To make a 4D plot, we can create x, y, z and c standard data points. Create a new figure or activate an existing figure.StepsUse figure() method to create a figure or activate an existing figure.Add a figure as part of a subplot arrangement.Create x, y, z and c data points using numpy.Create a scatter plot using scatter method.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 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = np.random.standard_normal(100) y = np.random.standard_normal(100) z = np.random.standard_normal(100) c = np.random.standard_normal(100) img = ax.scatter(x, ... Read More

How to plot a very simple bar chart (Python, Matplotlib) using input *.txt file?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:25:59

715 Views

To plot a very simple bar chart from an input text file, we can take the following steps −Make an empty list for bar names and heights.Read a text file and iterate each line.Append names and heights into lists.Plot the bar using lists (Step 1).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 bar_names = [] bar_heights = [] for line in open("test_data.txt", "r"):    bar_name, bar_height = line.split()    bar_names.append(bar_name)    bar_heights.append(bar_height) plt.bar(bar_names, bar_heights) plt.show()"test_data.txt" contains the following data −Javed 75 Raju 65 Kiran 55 Rishi 95OutputRead More

How to make two histograms have the same bin width in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:20:36

2K+ Views

To make two histograms having same bin width, we can compute the histogram of a set of data.StepsCreate random data, a, and normal distribution, b.Initialize a variable, bins, for the same bin width.Plot a and bins using hist() method.Plot b and bins using hist() 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 a = np.random.random(100) * 0.5 b = 1 - np.random.normal(size=100) * 0.1 bins = 10 bins = np.histogram(np.hstack((a, b)), bins=bins)[1] plt.hist(a, bins, edgecolor='black') plt.hist(b, bins, edgecolor='black') plt.show()OutputRead More

How to increase/reduce the fontsize of X and Y tick labels in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:16:36

2K+ Views

To increase/reduce the fontsize of x and y tick labels in matplotlib, we can initialize the fontsize variable to reduce or increase font size.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 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"]) using set_xticklabels() and set_yticklabels() with fontsize variable.To add space between axes and tick labels, we can use tick_params() method with pad argument that helps to ... Read More

How to plot a rectangle inside a circle in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:12:51

529 Views

To plot a rectangle inside a circle in matplotlib, we can take the following steps −Create a new figure or activate an existing figure using figure method.Add a subplot to the current axis.Make a rectangle and a circle instance using Rectangle() and Circle() class.Add a patch on the axes.Scale x and y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) rect1 = patches.Rectangle((-2, -2), 4, 2, color='yellow') circle1 = matplotlib.patches.Circle((0, 0), radius=3, color='red') ax.add_patch(circle1) ax.add_patch(rect1) plt.xlim([-5, 5]) plt.ylim([-5, 5]) plt.axis('equal') plt.show()OutputRead More

What is the difference betweent set_xlim and set_xbound in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:09:52

307 Views

set_xlim − Set the X-axis view limits.set_xbound − Set the lower and upper numerical bounds of the X-axis.To set the xlim and xbound, we can take the following steps −Using subplots(2), we can create a figure and a set of subplots. Here, we are creating 2 subplots.Create x and y data points using numpy.Use axis 1 to plot x and y data points using plot() method.Set x limit using set_xlim() method.Use axis 2 to plot x and y data points using plot() method.Sex xbound using set_xbound() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] ... Read More

Advertisements