Just by using plt.ylabel(rotation='horizontal'), we can align a label according to our requirement.StepsPlot the lines using [0, 5] and [0, 5] lists.Set the y-label for Y-axis, using ylabel method by passing rotation='horizontal'.Set the x-label for X-axis, using xlabel method.To show the plot, use plt.show() method.Examplefrom matplotlib import pyplot as plt plt.plot([0, 5], [0, 5]) plt.ylabel("Y-axis ", rotation='horizontal') plt.xlabel("X-axis ") plt.show()Output
In this program, we will perform inverse zero 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 inverse zero thresholding, pixels having intensity value greater than the threshold value are set to 0.Original ImageAlgorithmStep 1: Import cv2. Step 2: Define the threshold and max_val. Step 3: Pass these parameters in the cv2.threshold value and specify the type of ... Read More
In this program, we will find the edges in an image using the pillow library. The FIND_EDGES function in the ImageFilter class helps us to find the edges in our image.Original ImageAlgorithmStep 1: Import Image and ImageFilter from Pillow. Step 2: Open the image. Step 3: Call the filter function and specify the find_edges function. Step 4: Display the output.Example Codefrom PIL import Image, ImageFilter im = Image.open('testimage.jpg') im = im.filter(ImageFilter.FIND_EDGES) im.show()Output
By creating a 3D projection on the axis and iterating that axis for different angles using view_init(), we can rotate the output diagram.StepsCreate a new figure, or activate an existing figure.Add an `~.axes.Axes` to the figure as part of a subplot arrangement with nrow = 1, ncols = 1, index = 1, and projection = '3d'.Use the method, get_test_data to return a tuple X, Y, Z with a test dataset.Plot a 3D wireframe with data test data x, y, and z.To make it rotatable, we can set the elevation and azimuth of the axes in degrees (not radians), using view_init() ... Read More
First, we can create bars using plt.bar and using xticks. Then, we can align the labels by setting the “vertical” or “horizontal” attributes in the “rotation” key.StepsMake lists, bars_heights, and bars_label, with numbers.Make a bar plot using bar() method, with bars_heights and length of bars_label.Get or set the current tick locations and labels of the X-axis, using xticks() with rotation='vertical' and bars_label.To show the plot, use plt.show() method.Examplefrom matplotlib import pyplot as plt bars_heights = [14, 8, 10] bars_label = ["A label", "B label", "C label"] plt.bar(range(len(bars_label)), bars_heights) plt.xticks(range(len(bars_label)), bars_label, rotation='vertical') plt.show()OutputRead More
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
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
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
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]
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