Found 1034 Articles for Matplotlib

How to animate a scatter plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:54:57

5K+ Views

Using the FuncAnimation method of matplotlib, we can animate the diagram. We can pass a user defined method where we will be changing the position of the particles, and at the end, we will return plot type.StepsGet the particle's initial position, velocity, force, and size.Create a new figure, or activate an existing figure with figsize = (7, 7).Add an axes to the current figure and make it the current axes, with xlim and ylim.Plot scatter for initial position of the particles.Make an animation by repeatedly calling a function *func*. We can pass a user-defined method that helps to change the ... Read More

Python xticks in subplots

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:50:15

4K+ Views

Subplot can split the figure in nrow*ncols parts and plt.xticks could help to plot the xticks for subplots.StepsCreate two lists for line 1 and line 2.Add a subplot to the current figure, nrow = 1, ncols = 2 and index = 1.Draw line 1 with style as dashed.Set or retrieve auto scaling margins(0.2).Place xticks at even places.Set a title for the X-axis.Add a subplot to the current figure, nrow = 1, ncols = 2 and index = 2.Plot line 2.Set or retrieve auto scaling margins(0.2).Place xticks at odd places.Set a title for the X-axis.To show the figure use plt.show() method.Exampleimport ... Read More

How to remove lines in a Matplotlib plot?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:50:44

6K+ Views

We will create two lines, i.e., line1 and line2. After that, we will pop the second line and will remove it.StepsCreate lists for line1 and line2.Plot line1 and line 2 using plot() method, line 2 with style =’dashed’.Set or retrieve auto-scaling margins(0.2).Pop line 2, and remove it using remove() method.The final figure will have only one line, so use plt.show() method.Exampleimport matplotlib.pyplot as plt line1 = [2, 4, 8] line2 = [3, 6, 12] plt.plot(line1) line_2 = plt.plot(line2, linestyle='dashed') plt.margins(0.2) plt.title("With extra lines") plt.show() plt.plot(line1) l = line_2.pop(0) l.remove() plt.margins(0.2) plt.title("Removed extra lines") plt.show()Input Diagram (Before ... Read More

How can I make a scatter plot colored by density in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:49:08

1K+ Views

We can create a dict for color and a value. If the same value comes up, we can use a scatter method and if the closer values have the same set of colors, that could make the plot color denser.StepsCreate a new figure, or activate an existing figure.Add an ~.axes.Axes to the figure as part of a subplot arrangement.Get the x and y values using np.random.normal() method. Draw random samples from a normal (Gaussian) distribution.Make a color list with red and blue colors.To make it denser, we can store the same color with the same value.Plot scatter point, a scatter ... Read More

How can I plot a histogram such that the heights of the bars sum to 1 in matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:43:44

369 Views

In plt.hist() method, stacked=True could help to get the heights of the bars sum to 1.StepsCreate a list of numbers.Using plt.hist(), we can draw the histogram.stacked : bool, default: FalseIf "True", multiple data are stacked on top of each other If ``False`` multiple data are arranged side by side if histtype is 'bar' or on top of each other if histtype is 'step'.density : bool, default: FalseIf "True", draw and return a probability density: each bin will display the bin's raw count divided by the total number of counts *and the bin width*.To show the figure, use the plt.show() method.Examplefrom ... Read More

How can I plot NaN values as a special color with imshow in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:42:30

841 Views

First, we can create an array matrix with some np.nan value, and using imshow method, we can create a diagram for that matrix.StepsCreate a new figure, or activate an existing figure.Add an `~.axes.Axes` to the figure as part of a subplot arrangement, nrows = 1, ncols = 1, index = 1.Create a 2D array with np.nan.Display data as an image, i.e., on a 2D regular raster.Use the draw() method which draws the drawing at the given location.To show the figure, use the plt.show() method.Exampleimport numpy as np import matplotlib.pyplot as plt f = plt.figure() ax = f.add_subplot(111) a = ... Read More

How to get a matplotlib Axes instance to plot to?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:41:32

409 Views

To get the axes instance, we will use the subplots() method.StepsMake a list of years.Make a list of populations in that year.Get the number of labels using np.arrange(len(years)) method.Set the width of the bars.Create fig and ax variables using subplots() method, where default nrows and ncols are 1.Set the Y-axis label of the figure using set_ylabel().Set the title of the figure, using set_title() method.Set the x-ticks with x that is created in step 3, using set_xticks method.Set the xtick_labels with years data, using set_xticklabels method.Use plt.show() method to show the figure.Examplefrom matplotlib import pyplot as plt import numpy as np ... Read More

How to force the Y axis to use only integers in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:37:29

4K+ Views

Whenever Y value list will be made, then we will convert those datasets into a new list, with ceil and floor value of the given list accordingly. Then, we can plot the graph for the new list data.StepsTake an input list.Find the minimum and maximum values in the input list (Step 1).Create a range between min and max value (Step 2).Get or set the current tick locations and labels of the Y-axis, with a new list.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Set a title for the axes.To show the figure we can use the ... Read More

Which is the recommended way to plot – matplotlib or pylab?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:36:48

180 Views

pylab is a module that imports matplotlib.pyplot (for plotting) and numpy (for mathematics and working with arrays) in a single namespace.Although many examples use pylab, it is no longer recommended. For non-interactive plotting, it is suggested to use pyplot to create the figures and then the OO interface for plotting.Exampleimport matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2, 100) plt.plot(x, x, label='linear') plt.plot(x, x**2, label='quadratic') plt.plot(x, x**3, label='cubic') plt.xlabel('x label') plt.ylabel('y label') plt.title("Simple Plot") plt.legend() plt.show()Output

Change grid interval and specify tick labels in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:36:26

3K+ Views

Using plt.figure() method, we can create a figure and thereafter, we can create an axis. Using set_xticks and set_yticks, we can change the ticks format and ax.grid could help to specify the grid interval.StepsCreate a new figure, or activate an existing figure, using fig = plt.figure() method.Add an `~.axes.Axes` to the figure as part of a subplot arrangement, where nrow = 1, ncols = 1 and index = 1.Get or set the current tick locations and labels of the X-axis.Get or set the current tick locations and labels of the X-axis. With minor = True, Grid.Get or set the current ... Read More

Advertisements