Remove Horizontal Lines in Image using OpenCV and Matplotlib

Rishikesh Kumar Rishi
Updated on 05-Jun-2021 07:41:47

3K+ Views

To remove horizontal lines in an image, we can take the following steps −Read a local image.Convert the image from one color space to another.Apply a fixed-level threshold to each array element.Get a structuring element of the specified size and shape for morphological operations.Perform advanced morphological transformations.Find contours in a binary image.Repeat step 4 with different kernel size.Repeat step 5 with a new kernel from step 7.Show the resultant image.Exampleimport cv2 image = cv2.imread('input_image.png') cv2.imshow('source_image', image) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 1)) detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, ... Read More

Specify Line Width of Legend Frame in Matplotlib

Rishikesh Kumar Rishi
Updated on 05-Jun-2021 07:39:47

4K+ Views

To specify the line width of the legend frame in Matplotlib, we can use set_linewidth() method.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots using subplots() method.Plot x and y using plot() method.Place a legend on the figure and get the legend instance.Get the lines and set the line width in the legend frame.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(-5, 5, 100) y ... Read More

Plot Pandas Multi-Index DataFrame with All X-Ticks in Matplotlib

Rishikesh Kumar Rishi
Updated on 05-Jun-2021 07:38:30

5K+ Views

To plot a Pandas multi-index data frame with all xticks, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create index value with 1000 smaples data.Make a one-dimensional ndarray with axis labels.Get the mean value of the series.Plot g dataframe.Set the ticks and ticklabel on the current axesTo display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True idx = pd.date_range("2020-01-01", periods=1000) val = np.random.rand(1000) s = pd.Series(val, idx) g = s.groupby([s.index.year, s.index.month]).mean() ... Read More

Use Pyplot Without Display

Rishikesh Kumar Rishi
Updated on 05-Jun-2021 07:36:37

877 Views

We can save the current figure in the local machine and can display it.StepsSet the figure size and adjust the padding between and around the subplots.Create x data points using numpy.Plot x and y data points using plot() method.Save the figure using savefig() 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(-np.pi, np.pi, 100) plt.plot(x, np.sin(x) * x, c='red') plt.savefig("myimage.png")OutputWhen we execute the code, it will save the following image as "myimage.png" in the project directoryRead More

Display Real-Time Graphs in a Simple UI for a Python Program

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 13:50:18

844 Views

To display real-time graphs in a simple UI for a Python program, we can animate the contour plot.StepsSet the figure size and adjust the padding between and around the subplots.Create a random data of shape 10×10 dimension.Create a figure and a set of subplots using subplots() method.Make an animation by repeatedly calling a function *func*, using FuncAnimation() class.To update the contour value in a function, we can define a method, animate(), that can be used in FuncAnimation() class.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation plt.rcParams["figure.figsize"] = [7.50, 3.50] ... Read More

Draw a Line Outside of an Axis in Matplotlib

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:39:48

3K+ Views

To draw a line (i.e., arrow) outside of an axis, we can use annotate() method, StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure() method.Clear the current figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.Use annotate() method to place a line outside the axes.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 fig = plt.figure(1) fig.clf() ax = fig.add_subplot(1, 1, 1) ax.annotate('', xy=(0, -0.1), xycoords='axes fraction', xytext=(1, -0.1), ... Read More

Use Multiple Font Sizes in One Label in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:35:58

969 Views

To use multiple font sizes in one label in Python, we can use fontsize in title() 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 using plot() method.Initialize a variable, fontsize.Set the title of the plot using title() method with fontsize in the argument.Turn off the axes.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(-5, 5, 100) y = np.cos(x) plt.plot(x, y) fontsize = 20 ... Read More

Center Origin of Cosine Curve in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:34:30

1K+ Views

To put the Origin at the center of the cos curve in a figure, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Set the position of the axes using spines, top, left, right and bottom.Plot x and y data points using plot() method.Set the title of the plot.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(-5, 5, 100) y = np.cos(x) ax = plt.gca() ... Read More

Specify Arrow-Like Linestyle in Matplotlib

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:33:07

1K+ Views

To draw an arrow-like linestyle in matplotlib, we can use quiver() method.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Use quiver() method to draw a line.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(-5, 5, 100) y = np.sin(x) plt.quiver(x[:-1], y[:-1], x[1:]-x[:-1], y[1:]-y[:-1], scale_units='xy', angles='xy', scale=1, color='red') plt.show()Output

Plot a Vector Field Over the Axes in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:31:57

836 Views

To plot a vector field over the axes in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make X, Y, T, R, U and V data points using numpy.Add an axes to the current figure and make it the current axes.Plot a 3D field of arrows using quiver() 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 n = 8 X, Y = np.mgrid[0:n, 0:n] T = np.arctan2(Y - n / 2., X - n/2.) ... Read More

Advertisements