Found 10476 Articles for Python

Show Matplotlib graphs to image as fullscreen

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

16K+ 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

7K+ 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

896 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 plot a rectangle inside a circle in Matplotlib?

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

759 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

518 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

How to animate a pcolormesh in Matplotlib?

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

3K+ 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

4K+ 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

4K+ 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

39K+ 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

Advertisements