Setting the Size of the Plotting Canvas in Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:32:11

3K+ Views

To set the size of the plotting canvas in matplotlib, we can take the following steps:Set the figure size and adjust the padding between and around the subplots. Use figsize 7.50 and 3.50 to set the figure size.Create x and y data points using numpy.Plot x and y data points using plot() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 100) y = np.sin(x) plt.plot(x, y) plt.show()Output

Default Seaborn Color Palette

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:29:38

359 Views

To get the default Seaborn color palette, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Return a list of colors or continuous colormap defining a palette.Plot the values in a color palette as a horizontal array.To display the figure, use show() method.Exampleimport seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True current_palette = sns.color_palette() sns.palplot(current_palette) plt.show()Output

Different Font Sizes in Matplotlib Annotations

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:28:26

3K+ Views

To add different font sizes in the same annotation method, we can take the following stepsMake lists of x and y data points where text could be placed.Initialize a variable 'labels', i.e., a string.Make a list of sizes of the fonts.Use subplots() method to create a figure and a set of subplots.Iterate above lists and annotate each label's text and set its fontsize.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True X = [0.1, .2, .3, .4, .5, .6, 0.8] Y = [0.1, 0.12, 0.13, 0.20, 0.23, 0.25, 0.27] labels = 'Welcome' ... Read More

Load and Show Image Using Keras

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:26:28

2K+ Views

To load an image and show the image using Keras, we will use load_image() method to load an image and set the target size of the image to be shown.StepsUse load_img() method to load the figure.Set the target size of the image.To display the figure, use show() method.Examplefrom keras.preprocessing import image img = image.load_img('bird.jpg', target_size=(350, 750)) img.show()Output

Adjust Spacing Between Plot Edge and X-Axis in Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:24:21

3K+ Views

To adjust the spacing between the edge of the plot and the X-axis, we can use tight_layout() method or set the bottom padding of the current figure.Set the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points using plot() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 100) y = np.exp(x) plt.plot(x, y, c='red', lw=1) plt.show()OutputRead More

Add Footnote Under X-Axis Using Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:21:06

3K+ Views

To add footnote under the X-axis using matplotlib, we can use figtext() and text() method.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points using numpy.To place the footnote, use figtext() method with x, y position and box properties.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 100) y = np.exp(x) plt.plot(x, y) plt.figtext(0.5, 0.01, "footnote: $y=e^{x}$", ha="center", fontsize=18, bbox={"facecolor": "green", "alpha": 0.75, "pad": 5}) plt.show()OutputRead More

Plot YScale Class Linear, Log, Logit, and Symlog in Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:17:35

561 Views

To plot yscale with class by name, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create y data points using numpy.Create x data points using numpy.Add a subplot to the current figure at index 1.Plot x and y data points using plot() method.For linear class by name, use yscale("linear") method.Set the title of the current subplot.Repeat the steps from 4 to 5 with different indices, yscale() class by name, and title of the plot.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] ... Read More

Locate the Median in a Seaborn KDE Plot

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:14:40

2K+ Views

To locate the median in a seaborn KDE plot, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create random data using numpy.Find the median of data (Step 2).Use kdeplot() to plot the shaded region.Use axvline() method to plot the vertical line.To display the figure, use show() method.Exampleimport numpy as np import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randn(30) xmedian = np.median(data) k = sns.kdeplot(x=data, shade=True) plt.axvline(xmedian, c='red') plt.show()OutputRead More

Set Margins of a Matplotlib Figure

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:06:23

6K+ Views

To set the margins of a matplotlib figure, we can use margins() method.StepsSet the figure size and adjust the padding between and around the subplots.Create t and y data points using numpy.Add a subplot to the current figure at index 1.Plot t and y data points using plot() method.Set the title of the plot.Add a subplot to the current figure at index 2.Plot t and y data points using plot() method.Set the title of the plot.Set margins of the plot using margins(x=0, y=0).To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] ... Read More

Automatically Set Y-Axis Limits for Bar Graph Using Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:00:21

9K+ Views

To set Y-axis limit, we can use ylim() method and put maximum and minimum limit values.StepsSet the figure size and adjust the padding between and around the subplots.Create two lists for data points.Make two variables for max and min values for Y-axis.Use ylim() method to limit the Y-axis range.Use bar() method to plot the bars.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4, 5] y = [8, 4, 6, 1, 3] max_y_lim = max(y) + .5 min_y_lim = min(y) plt.ylim(min_y_lim, max_y_lim) plt.bar(x, y) ... Read More

Advertisements