Found 1034 Articles for Matplotlib

Change figure size and figure format in Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:43:42

729 Views

Using the figsize attribute of figure(), we can change the figure size. To change the format of a figure, we can use the savefig method.StepsStore the figure size in the variable.Create a new figure, or activate an existing figure, with given figure size.Plot the line using x.Set the image title with its size.Save the figure using savefig() method.Examplefrom matplotlib import pyplot as plt figure_size = (10, 10) plt.figure(figsize=figure_size) x = [1, 2, 3] plt.plot(x, x) plt.title("Figure dimension is: {}".format(figure_size)) plt.savefig("imgae.png", format="png")Output

How can I get the output of a Matplotlib plot as an SVG?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:44:26

14K+ Views

Just using the savefig method of the pyplot package and mentioning the file format, we can save the output as a SVG format.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Create xpoints and ypoints using np.array(0, 5).Plot lines using xpoints and ypoints.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.To save the file in SVG format, use savefig() method where image name is myImagePDF.svg, format="svg".To show the image, use plt.show() method.Exampleimport matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() xpoints = np.array([0, 5]) ypoints = np.array([0, ... Read More

Show Matplotlib plots (and other GUI) in Ubuntu

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:28:38

1K+ Views

Use the plot method of matplotlib and set the legend with different sets of colors.StepsSet the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Plot the lines using plt.plot() method with [9, 5], [2, 5] and [4, 7, 8] array.Initialize two variables; location = 0 for the best location and border_drawn_flag = True (True, if border to be drawn for legend. False, if border is not drawn).Use plt.legend() method for the legend and set the location and border_drawn_flag accordingly to get the perfect legend in the diagram.Show the figure using plt.show() method.Exampleimport matplotlib.pyplot as plt plt.ylabel("Y-axis ") ... Read More

How can I cycle through line styles in Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:27:19

244 Views

To plot multiple lines in a diagram, we can use the cycler that could help to set a new color from the given list of colors. (Here, ‘r’ => ‘red’, ‘g’ => ‘green’, ‘y’ => ‘yellow’, ‘b’ => ‘blue’).StepsUse a cycler to set the color for the group of lines. The color list consists of ‘r’ for red, ‘g’ for green, ‘b’ for blue, and ‘y’ for yellow.The cycler class helps to create a new Cycler object from a single positional argument, a pair of positional arguments, or the combination of keyword arguments.Plot the number of lines with different colors.Use ... Read More

Putting a newline in Matplotlib label with TeX in Python

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:25:47

3K+ Views

The following program code shows how you can plot a newline in matplotlib label with Tex.StepsSetup X-axis and Y-axis labels for the diagram with to plot a newline in the labels.Set the current .rcParams for axes facecolor; the group is axed.Use a cycler to set the color for the group of lines. The color list consists of ‘r’ for red, ‘g’ for green, ‘b’ for blue, and ‘y’ for yellow.The cycler class helps to create a new Cycler object from a single positional argument, a pair of positional arguments, or the combination of keyword arguments.Plot the number of lines ... Read More

Generating a movie from Python without saving individual frames to files

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:14:06

287 Views

Using the FuncAnimation method, we can create a film. We will create a user-defined method, update, to keep on changing the position of particles and at the end, the method would return the scatter instance.StepsGet the particles 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 the scatter for initial position of the particles.Makes an animation by repeatedly calling a function *func*. We can pass a user-defined method that helps to change the position ... Read More

Using Colormaps to set the color of line in Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:16:54

1K+ Views

First, we can create a list of colors, and then, we can use the colors while plotting a line in a loop.StepsReturn evenly spaced numbers over a specified interval, store in x.Update x for four lines and get another variable for evenly_spaced_interval.Make a list of colors.Iterate color and set color for all the lines.To show the figure, use plt.show() method.Examplefrom matplotlib import pyplot as plt, cm import numpy as np x = np.linspace(0, 10, 100) lines = [x, x+10, x+5, x+11] evenly_spaced_interval = np.linspace(0, 1, len(lines)) colors = [cm.rainbow(x) for x in evenly_spaced_interval] for i, color in enumerate(colors): ... Read More

How to prevent numbers being changed to exponential form in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:17:18

698 Views

Using style='plain' in the ticklabel_format() method, we can restrict the value being changed into exponential form.StepsPass two lists to draw a line using plot() method.Use ticklabel_format() method with style='plain'. If a parameter is not set, the corresponding property of the formatter is left unchanged. Style='plain' turns off scientific notation.To show the figure, use plt.show() method.Examplefrom matplotlib import pyplot as plt plt.plot([1, 2, 3, 4, 5], [11, 12, 13, 14, 15]) plt.ticklabel_format(style='plain')    # to prevent scientific notation. plt.show()Output

Drawing average line in histogram in Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:18:07

2K+ Views

We can plot some expressions using the hist method. After that, we will plot the average graph for the expression using the plot method and bins that are returned while creating the hist.StepsGet the data for x using some equations, set num_bins = 50.Create fig and ax variables using subplots method, where default nrows and ncols are 1.Get n, bins, patches value using ax.hist() method.Plot average lines using bins and y data that is obtained from some equations.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Set a title for the axes.Using tight_layout(), we can adjust the ... Read More

Plotting dates on the X-axis with Python's Matplotlib

Rishikesh Kumar Rishi
Updated on 28-Aug-2023 12:43:55

27K+ Views

Using Pandas, we can create a dataframe and can set the index for datetime. Using gcf().autofmt_xdate(), we will adjust the date on the X-axis.StepsMake the list of date_time and convert into it in date_time using pd.to_datetime().Consider data = [1, 2, 3]Instantiate DataFrame() object, i.e., DF.Set the DF['value'] with data from step 2.Set DF.index() using date_time from step 1.Now plot the data frame i.e., plt.plot(DF).Get the current figure and make it autofmt_xdate().Using plt.show() method, show the figure.Exampleimport pandas as pd import matplotlib.pyplot as plt date_time = ["2021-01-01", "2021-01-02", "2021-01-03"] date_time = pd.to_datetime(date_time) data = [1, 2, 3] DF = ... Read More

Advertisements