Found 26504 Articles for Server Side Programming

How to change the color of the axis, ticks and labels for a plot in matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Oct-2023 03:00:48

40K+ Views

We can change the color of the axis, ticks and labels, using ax.spines['left'].set_color('red') and ax.spines['top'].set_color('red') statements. To change the color of the axis, ticks, and labels for a plot in matplotlib, we can take the following steps −Create a new figure, or activate an existing figure, using plt.figure().Add an axis to the figure as part of a subplot arrangement, using plt.add_subplot(xyz) where x is nrows, y is ncols and z is the index. Here taking x = 1(rows), y = 2(columns) and z = 1(position).Set up X-axis and Y-axis labels using set_xlabel and set_ylabel method for creating ax using add_subplot().To ... Read More

What is the difference between drawing plots using plot, axes or figure in matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:49:11

3K+ Views

Let’s understand the difference between plot, axes, and figure with an example.Plot − Plot helps to plot just one diagram with (x, y) coordinates.Axes − Axes help to plot one or more diagrams in the same window and sets the location of the figure.Figure − This method provides a top-level container for all the plot elements.We can follow these steps to replicate the differences among them −Create a new figure, or activate an existing figure, using plt.figure().Add an axis to the figure as part of a subplot arrangement, using plt.add_subplot(xyz) where x is nrows, y is ncols and z is ... Read More

Plotting with seaborn using the matplotlib object-oriented interface

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:46:37

289 Views

Seaborn is used to visualizing the random distribution and we can use matplotlib interface to show this distribution over a diagram.We can take the following steps to show the diagram −Figure level interface for drawing distribution plots onto a Face Grid. This function provides access to several approaches for visualizing the univariate or bivariate distribution of data, including subsets of data defined by semantic mapping and faceting across multiple subplots.List of numbers can be passed in the above-defined method, i.e., displot().To show the diagram, plt.show() can be used whereas plot was drawn using Seaborn.Exampleimport matplotlib.pyplot as plt import seaborn as ... Read More

How to set the matplotlib figure default size in ipython notebook?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:45:08

5K+ Views

To set the matplotlib figure default size in iPython, use the following steps −To check the default figure size, use plt.rcParams["figure.figsize"] over the ipython shell.Now to set the figure size, override the plt.rcParams["figure.figsize"] variable with a tuple i.e., (20, 10).After overriding the plt.rcParams["figure.figsize"] variable, you can use it to get changed figure size.Exampleimport matplotlib.pyplot as plt print("Before, figure default size is: ", plt.rcParams["figure.figsize"]) plt.rcParams["figure.figsize"] = (20, 10) print("After, figure default size is: ", plt.rcParams["figure.figsize"])OutputBefore, figure default size is: [6.4, 4.8] After, figure default size is: [20.0, 10.0]Read More

Fill between two vertical lines in matplotlib

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:43:33

2K+ Views

To fill color between two vertical lines, use the following steps −Using plt.subplots() method, create a figure and a set of subplots. This utility wrapper makes it convenient to create common layouts of subplots, including the enclosing figure object, in a single call.To draw two vertical lines, initialize x = 3 and x = 5.Using the created ax, axvspan would help to add vertical span(rectangle) across the axes.This rectangle spans from xmin to xmax horizontally, and, by default, the whole Y-axis vertically.To show the figure, use the plt.show() method.Exampleimport matplotlib.pyplot as plt fig, ax = plt.subplots() line1 = ... Read More

Remove or adapt the border of the frame of legend using matplotlib

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:41:58

2K+ Views

To remove or adapt the border of the frame of legend we can follow the following steps −Set the X-axis label using the plt.xlabel() method.Set the Y-axis label using the plt.ylabel() method.Plot the lines using the plt.plot() method with [9, 5], [2, 5] and [4, 7, 8] array.Initializing 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 the plt.legend() method for the legend and set the location and border_drawn_flag accordingly to get the perfect legend in the diagram.plt.show() method would help to ... Read More

How to plot a histogram using Matplotlib in Python with a list of data?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:40:11

15K+ Views

To plot a histogram using Matplotlib, we can follow the steps given below −Make a list of numbers and assign it to a variable x.Use the plt.hist() method to plot a histogram.Compute and draw the histogram of *x*.We can pass n-Dimensional arrays in the hist argument also.To show the plotted figure, use the plt.show() method.Examplefrom matplotlib import pyplot as plt x = [300, 400, 500, 2000, 10] plt.hist(x, 10) plt.show()Output

How to display multiple images in one figure correctly in matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:38:35

5K+ Views

To display multiple images in one figure, we can follow the steps given below −Initialize the number of rows and cols. nrows*ncols subplot will be created in the current figure. nrows = 2 and ncols = 2, i.e., 2*2 = 4 subplots can be created.Now add the figures at different indices from 1 to 4.Use plt.subplot(2, 2, 1) to add new images, i.e., pie at index 1.To plot a pie chart, pass a list of numbers. Pie charts will be split into the size of list and %age section will depend upon the values in the list.Set the title of ... Read More

How to pick a new color for each plotted line within a figure in matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:37:02

1K+ Views

To pick a new color for each plotted line within a figure, use the following steps −Setup X-axis and Y-axis labels for the diagram.Set the current .rc parameters. For axes facecolor, the group is axes.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.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 plt.show() to show the figure.Exampleimport ... Read More

Hide axis values but keep axis tick labels in matplotlib

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:34:08

8K+ Views

To hide the axis value but to keep the axis tick labels, we can perform the following steps −Plot a line using the plot( ) method.Set X and Y labels using x label and y label methods.Using plt.gca(), get the current axis, creating one if necessary.Use xaxis.set_ticklabels() with an empty list.Use yaxis.set_ticklabels() with an empty list.To show the diagram, use the plt.show() method.Exampleimport matplotlib.pyplot as plt plt.plot([0, 5], [0, 5]) plt.ylabel("Y-axis ") plt.xlabel("X-axis ") ax = plt.gca() ax.axes.xaxis.set_ticklabels([]) ax.axes.yaxis.set_ticklabels([]) plt.show()OutputRead More

Advertisements