Found 10476 Articles for Python

How to change a table's fontsize with matplotlib.pyplot?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:17:24

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

Centering x-tick labels between tick marks in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:17:52

963 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

Changing Matplotlib subplot size/position after axes creation

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:16:58

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

How to rotate Matplotlib annotation to match a line?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:16:25

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

How do I close all the open pyplot windows (Matplotlib)?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:15:48

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.

How to retrieve colorbar instance from figure in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:10:26

718 Views

To retrieve colorbar instance from figure in matplotlib, we can use imshow scalar mappable object in colorbar to retrieve colorbar instance.StepsGet random data with 10×10 dimension of array, data points between -1 to 1.Use imshow() method to display data as an image, i.e., on a 2D regular raster.Create a colorbar for a ScalarMappable instance, *mappable*, with imshow() object.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 data = np.random.randint(-1, 1, (10, 10)) im = plt.imshow(data, interpolation="nearest") cbar = plt.colorbar(im) plt.show()OutputRead More

Rotating axis text for each subplot in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:10:01

443 Views

To rotate axis text for each subplot, we can use text with rotation in the argument.StepsCreate a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.Adjust the subplot layout parameters using subplots_adjust() method.Add a centered title to the figure using suptitle() method.Set the title of the axis.Set the x and y label of the plot.Create the axis with some co-ordinate points.Add text to the figure with some arguments like fontsize, fontweight and add rotation.Plot a single point and annotate that point with some text and arrowhead.To display the ... Read More

Layering a contourf plot and surface_plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:09:22

207 Views

To layer a contourf plot and surface_plot in matplotlib, we can take the following Steps −Initialize the variables, delta, xrange, yrange, x and y using numpy.Create a new figure or activate an existing figure using figure() method.Get the current axis where projection='3d'.Create a 3d countour plot with x and y data points.Plot the surface with x and y data points.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 delta = 0.025 xrange = np.arange(-5.0, 20.0, delta) yrange = np.arange(-5.0, 20.0, delta) x, y = np.meshgrid(xrange, yrange) ... Read More

How to make a heatmap square in Seaborn FacetGrid using Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:08:59

699 Views

To make a heatmap square in Seaborn facetgrid, we cn use heatmap() method with 10×10 random data set.StepsCreate a random data of size 10×10, with minimum -1 and maximum 10.Plot rectangular data as a color-encoded matrix using heatmap() method with data and color map "twilight_r".To display the figure, use show() method.Exampleimport numpy as np import seaborn as sn import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randint(low=-1, high=10, size=(10, 10)) hm = sn.heatmap(data=data, cmap="twilight_r") plt.show()Output

Plotting points on the surface of a sphere in Python's Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:08:19

14K+ Views

To plot points on the surface of a sphere in Python, we can use plot_surface() method.StepsCreate a new figure or activate an existing figure using figure() method.Add a set of subplots using add_subplot() method with 3d projection.Initialize a variable, r.Get the theta value for spherical points and x, y, and z data points using numpy.Plot the surface using plot_surface() method.To display the figure, use show() method.Exampleimport matplotlib.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(projection='3d') r = 0.05 u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j] x = np.cos(u) * ... Read More

Advertisements