Found 33676 Articles for Programming

Change figure size and figure format in Matplotlib

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

1K+ 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

17K+ 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

Performing binary thresholding on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:44:44

762 Views

In this program, we will perform binary thresholding on an image using openCV.Thresholding is a process in which the value of each pixel is changed in relation to a threshold value. The pixel is given a certain value if it is less than the threshold and some other value if it is greater than the threshold. In binary thresholding, if the value of the pixel is less than the threshold, it will be given a 0 value, i.e., black. If it is greater than the threshold, it will be assigned 255, i.e., white.Original ImageAlgorithmStep 1: Import cv2. Step 2: Define ... Read More

Downsampling an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:28:17

7K+ Views

In this program, we will down sample an image. Downsampling is decreasing the spatial resolution while keeping the 2D representation of an image. It is typically used for zooming out of an image. We will use the pyrdown() function in the openCV library to complete this task.Original ImageAlgorithmStep 1: Fead the image. Step 2: Pass the image as a parameter to the pyrdown() function. Step 3: Display the output.Example Codeimport cv2 image = cv2.imread('testimage.jpg') print("Size of image before pyrDown: ", image.shape) image = cv2.pyrDown(image) print("Size of image after pyrDown: ", image.shape) cv2.imshow('DownSample', image)OutputSize of image before pyrDown:  (350, ... Read More

Calculating the mean of all pixels for each band in an image using the Pillow library

Prasad Naik
Updated on 17-Mar-2021 08:27:40

2K+ Views

In this program, we will calculate the mean of all the pixels in each channel using the Pillow library. There are a total three channels in an image and therefore, we will get a list of three values.Original ImageAlgorithmStep 1: Import the Image and ImageStat libraries. Step 2: Open the image. Step 3: Pass the image to the stat function of the imagestat class. Step 4: Print the mean of the pixels.Example Codefrom PIL import Image, ImageStat im = Image.open('image_test.jpg') stat = ImageStat.Stat(im) print(stat.mean)Output[76.00257724463832, 69.6674300254453, 64.38017448200654]

Show Matplotlib plots (and other GUI) in Ubuntu

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

2K+ 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 to save a figure remotely with pylab in Python?

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

549 Views

Using the savefig method of the pyplot package, we can save the figure remotely by specifying the location of the figure.StepsTo use a different backend, set it using matplotlib.use('Agg') method.Plot the lines using plot() method.Using savefig() method, we can save the image remotely, just putting the directory.To show the figure, use plt.show().Exampleimport matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt plt.plot([1, 2, 3]) plt.savefig("remotely_fig.png")Output

How can I cycle through line styles in Matplotlib?

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

422 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

Upsampling an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:27:02

4K+ Views

In this program, we will up sample an image. Up sampling is increasing the spatial resolution while keeping the 2D representation of an image. It is typically used for zooming in on a small region of an image. We will use the pyrup() function in the openCV library to complete this task.Original ImageAlgorithmStep 1: Read the image. Step 2: Pass the image as a parameter to the pyrup() function. Step 3: Display the output.Example Codeimport cv2 image = cv2.imread('testimage.jpg') print("Size of image before pyrUp: ", image.shape) image = cv2.pyrUp(image) print("Size of image after pyrUp: ", image.shape) cv2.imshow('UpSample', image)OutputSize ... Read More

Performing white BlackHat operation on images using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:26:42

386 Views

In this program, we will perform the Blackhat operation on an image using OpenCV. BlackHat transform is used to enhance dark objects of interest in a bright background. We will use the morphologyEx(image, cv2.MORPH_BLACKHAT, kernel) function.Original ImageAlgorithmStep 1: Import cv2. Step 2: Read the image. Step 3: Define the kernel size. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 5: Display the output.Example Codeimport cv2 image = cv2.imread('image_test.jpg') filter_size = (5,5) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filter_size) image = cv2.morphologyEx(image, cv2.MORPH_BLACKHAT, kernel) cv2.imshow('BlackHat', image)Output

Advertisements