 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Matplotlib Articles - Page 67 of 104
 
 
			
			8K+ Views
To plot a horizontal line on multiple subplots in Python, we can use subplots to get multiple axes and axhline() method to draw a horizontal line.StepsCreate a figure and a set of subplots. Here, we will create 3 subplots.Use axhline() method to draw horizontal lines on each axis.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt fig, (ax1, ax2, ax3) = plt.subplots(3) plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ax1.axhline(y=0.5, xmin=0, xmax=3, c="black", linewidth=2, zorder=0) ax2.axhline(y=0.5, xmin=0, xmax=3, c="red", linewidth=3, zorder=0) ax3.axhline(y=0.5, xmin=0, xmax=3, c="yellow", linewidth=4, zorder=0) plt.show()OutputRead More
 
 
			
			3K+ Views
To overlay an image segmentation with numpy, we can take the following Steps −Make a masked array of 10×10 dimension.Update the masked array with 1 for some region.Make image data using numpy.Mask an array where a condition is met, to get the masked data.Create a new figure or activate an existing figure using figure() mrthod.Use imshow() method to display data as an image, i.e., on a 2D regular raster.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 mask = np.zeros((10, 10)) mask[3:-3, 3:-3] = 1 im ... Read More
 
 
			
			2K+ Views
To limit the number of groups shown in a Seaborn countplot, we can use a variable group_count, used in countplot() method arguments.StepsCreate a figure and two sets of subplots.Create a data frame using Pandas, with two keys.Initalize a variable group_count to limit the group count in countplot() method.Use countplot() method to show the counts of observations in each categorical bin using bars.Adjust the padding between and around the subplots.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 = ... Read More
 
 
			
			2K+ Views
To plot a probability density function by sample, we can use numpy for x and y data points.StepsCreate x and p data points using numpy.Plot x and p data points using plot() method.Scale X-axis in a range.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 x = np.arange(-100, 100) p = np.exp(-x ** 2) plt.plot(x, p) plt.xlim(-20, 20) plt.show()Output
 
 
			
			8K+ Views
To plot a 2D matrix in Python with colorbar, we can use numpy to create a 2D array matrix and use that matrix in the imshow() method.StepsCreate data2D using numpy.Use imshow() method to display data as an image, i.e., on a 2D regular raster.Create a colorbar for a ScalarMappable instance *mappable* using colorbar() method and imshow() scalar mappable image.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 data2D = np.random.random((50, 50)) im = plt.imshow(data2D, cmap="copper_r") plt.colorbar(im) plt.show()OutputRead More
 
 
			
			8K+ Views
To change a table's fontsize with matplotlib, we can use set_fontsize() method.StepsCreate a figure and a set of subplots, nrows=1 and ncols=1.Create random data using numpy.Create columns value.Make the axis tight and off.Initialize a variable fontsize to change the font size.Set the font size of the table using set_font_size() 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 fig, axs = plt.subplots(1, 1) data = np.random.random((10, 3)) columns = ("Column I", "Column II", "Column III") axs.axis('tight') axs.axis('off') the_table = axs.table(cellText=data, colLabels=columns, loc='center') the_table.auto_set_font_size(False) the_table.set_fontsize(10) plt.show()OutputRead More
 
 
			
			981 Views
To place labels between two ticks, we can take the following steps−Load some sample data, r.Create a copy of the array, cast to a specified type.Create a figure and a set of subplots using subplots() method.Plot date and r sample data.Set the locator of the major/minor ticker using set_major_locator() and set_minor_locator() methods.Set the locator of the major/minor formatter using set_major_locator() and set_minor_formatter() methods.Now, place the ticklabel at the center.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.cbook as cbook import matplotlib.dates as dates import matplotlib.ticker as ticker import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = ... Read More
 
 
			
			1K+ Views
To change subplot size or position after axes creation, we can take the following steps−Create a new figure or activate an existing figure using figure() method.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.A grid layout to place subplots within a figure using GridSpec() class.Set the position of the grid specs.Set the subplotspec instance.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method, with gridspec instance.Adjust the padding between and around the subplots.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt from matplotlib import gridspec as ... Read More
 
 
			
			4K+ Views
To rotate matplotlib annotation to match a line, we can take the following steps−Create a new figure or activate an existing figure using figure() method.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.Initialize the variables, m (slope) and c (intercept).Create x and y data points using numpy.Calculate theta to make text rotation.Plot the line using plot() method with x and y.Place text on the line using text() 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 fig = plt.figure() ax ... Read More
 
 
			
			11K+ Views
plt.figure().close(): Close a figure window.close() by itself closes the current figureclose(h), where h is a Figure instance, closes that figureclose(num) closes the figure with number=numclose(name), where name is a string, closes the figure with that labelclose('all') closes all the figure windowsExamplefrom matplotlib import pyplot as plt fig = plt.figure() ax = fig.add_subplot() plt.show() plt.close()OutputNow, swap the statements "plt.show()" and "plt.close()" in the code. You wouldn't get to see any plot as the output because the plot would already have been closed.