Found 10476 Articles for Python

How to read an image in Python OpenCV?

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

988 Views

To read an image in Python OpenCV, we can take the following Steps −Load an image from a file.Display the image in the specified window.Wait for a pressed key.Destroy all of the HighGUI windows.Exampleimport cv2 img = cv2.imread("baseball.png", cv2.IMREAD_COLOR) cv2.imshow("baseball", img) cv2.waitKey(0) cv2.destroyAllWindows()Output

How to change the figsize for matshow() in Jupyter notebook using Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:31:37

923 Views

To change the figsize for mathshow, we can use figsize in figure method argument and use fignum in matshow() method.StepsCreate a new figure or activate an existing figure using figure() method.Create a dataframe using Pandas.Use matshow() method to display an array as a matrix in a new figure window.The argument fignum can take the values None, int, or FalseIf *None*, create a new figure window with automatic numbering.If a nonzero integer, draw into the figure with the given number. Create one, if it does not exist.If 0, use the current axes (or create one if it does not exist).To display ... Read More

How to get the default blue colour of matplotlib.pyplot.scatter?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:23:40

5K+ Views

The default color of a scatter point is blue. To get the default blue color of matplotlib scatter point, we can annotate them using annotate() method.StepsCreate a figure and a set of subplots using subplots() method.Plot a scatter point at (-1, 1) location.Add some label for that point.Plot a scatter point at (-0.9, 1) location.Add some label for that point.Plot a scatter point at (1.9, 1) location.Add some label for that point.Scale the x and y axes using xlim and ylim method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ... Read More

How to set same color for markers and lines in a Matplotlib plot loop?

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

1K+ Views

To set the same color for markers and lines in a matplotlib, we can take the following Steps −Initialize m, n and x data points using numpy.Create a new figure or activate an existing figure using figure() method.Clear the figure using clf() method.Add a subplot to the current figure using subplot() method.Get a marker from a iterable marker type.Iterate a range from 1 to n.Plot the lines and markers in the loop using plot() method with the same marker and colors for a line.To display the figure, use show() method.Exampleimport numpy as np import itertools from matplotlib import pyplot as ... Read More

Plot a rectangle with an edgecolor in Matplotlib

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

2K+ Views

To put edgecolor of a rectangle in matplotlib, we can take the following Steps −Create a new figure or activate an existing figure using figure() method.Add a subplot method to the current axis.Create a rectangle instance using Rectangle() class with an edgecolor and linewidth of the edge.Add a rectangle path on the plot.To place the text in the rectangle, we can use text() method.Scale x and y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) ... Read More

Plotting a horizontal line on multiple subplots in Python using pyplot

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

8K+ Views

To plot a horizontal line on multiple subplots in Python, we can use subplots to get multiple axes and axhline() method to draw a horizontal line.StepsCreate a figure and a set of subplots. Here, we will create 3 subplots.Use axhline() method to draw horizontal lines on each axis.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt fig, (ax1, ax2, ax3) = plt.subplots(3) plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ax1.axhline(y=0.5, xmin=0, xmax=3, c="black", linewidth=2, zorder=0) ax2.axhline(y=0.5, xmin=0, xmax=3, c="red", linewidth=3, zorder=0) ax3.axhline(y=0.5, xmin=0, xmax=3, c="yellow", linewidth=4, zorder=0) plt.show()OutputRead More

Overlay an image segmentation with Numpy and Matplotlib

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

2K+ Views

To overlay an image segmentation with numpy, we can take the following Steps −Make a masked array of 10×10 dimension.Update the masked array with 1 for some region.Make image data using numpy.Mask an array where a condition is met, to get the masked data.Create a new figure or activate an existing figure using figure() mrthod.Use imshow() method to display data as an image, i.e., on a 2D regular raster.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 mask = np.zeros((10, 10)) mask[3:-3, 3:-3] = 1 im ... Read More

How to limit the number of groups shown in a Seaborn countplot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:11:39

2K+ Views

To limit the number of groups shown in a Seaborn countplot, we can use a variable group_count, used in countplot() method arguments.StepsCreate a figure and two sets of subplots.Create a data frame using Pandas, with two keys.Initalize a variable group_count to limit the group count in countplot() method.Use countplot() method to show the counts of observations in each categorical bin using bars.Adjust the padding between and around the subplots.Exampleimport pandas as pd import numpy as np import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True f, axes = plt.subplots(1, 2) df = ... Read More

Plotting a probability density function by sample with Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:11:07

2K+ Views

To plot a probability density function by sample, we can use numpy for x and y data points.StepsCreate x and p data points using numpy.Plot x and p data points using plot() method.Scale X-axis in a range.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 x = np.arange(-100, 100) p = np.exp(-x ** 2) plt.plot(x, p) plt.xlim(-20, 20) plt.show()Output

How to plot a 2D matrix in Python with colorbar Matplotlib?

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

8K+ Views

To plot a 2D matrix in Python with colorbar, we can use numpy to create a 2D array matrix and use that matrix in the imshow() method.StepsCreate data2D using numpy.Use imshow() method to display data as an image, i.e., on a 2D regular raster.Create a colorbar for a ScalarMappable instance *mappable* using colorbar() method and imshow() scalar mappable image.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 data2D = np.random.random((50, 50)) im = plt.imshow(data2D, cmap="copper_r") plt.colorbar(im) plt.show()OutputRead More

Advertisements