Found 10476 Articles for Python

How can I display an image using cv2 in Python?

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:41:56

1K+ Views

To read an image in Python cv2, 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 create a standard colorbar for a series of plots in Python?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:44:46

438 Views

To create a standard colorbar for a series of plots, we can take the following steps −Create random data using numpy.Create a figure and a set of subplot using subplots() method, where nrows=1 and ncols=1.Display data as an image.Add an axes to the figure, for colorbar.Create a colorbar where mappable instance is image and cax where color will be drawn.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(4, 4) fig, ax = plt.subplots(nrows=1, ncols=1) im = ax.imshow(data) cax = fig.add_axes([0.9, 0.1, 0.03, 0.8]) fig.colorbar(im, cax=cax) ... Read More

Save figure as file from iPython notebook using Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:46:34

5K+ Views

To save a figure as a file from iPython, we can take the following steps−Create a new figure or activate an existing figure.Add an axes to the figure using add_axes() method.Plot the given list.Save the plot using savefig() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_axes([1, 1, 1, 1]) plt.plot([1, 2]) plt.savefig('test.png', bbox_inches='tight')OutputWhen we execute the code, it will save the following plot as "test.png".

Matplotlib – Plot over an image background in Python

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:26:18

20K+ Views

To plot over an image background, we can take the following steps−Read an image from a file into an array.Create a figure (fig) and add a set of subplots (ax) with extent [0, 300, 0, 300].Create an array x of range (300).Plot x using plot() method with linestyle=dotted, linewidth=2, and color=red.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 im = plt.imread("bird.jpg") fig, ax = plt.subplots() im = ax.imshow(im, extent=[0, 300, 0, 300]) x = np.array(range(300)) ax.plot(x, x, ls='dotted', linewidth=2, color='red') plt.show()OutputRead More

How to specify values on Y-axis in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:24:32

12K+ Views

To specify values on Y-axis in Python, we can take the following steps−Create x and y data points using numpy.To specify the value of axes, create a list of characters.Use xticks and yticks method to specify the ticks on the axes with x and y ticks data points respectively.Plot the line using x and y, color=red, using plot() method.Make x and y margin 0.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.array([0, 2, 4, 6]) y = np.array([1, 3, 5, 7]) ticks = ... Read More

Matplotlib – How to insert a degree symbol into a Python plot?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:19:02

11K+ Views

To insert a degree symbol into a plot, we can use LaTeX representation.StepsCreate data points for pV, nR and T using numpy.Plot pV and T using plot() method.Set xlabel for pV using xlabel() method.Set the label for temperature with degree symbol using ylabel() method.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 pV = np.array([3, 5, 1, 7, 10, 9, 4, 2]) nR = np.array([31, 15, 11, 51, 12, 71, 41, 13]) T = np.divide(pV, nR) plt.plot(pV, T, c="red") plt.xlabel("Pressure x Volume") plt.ylabel("Temperature ($^\circ$C)") plt.show()OutputRead More

How to get rid of grid lines when plotting with Seaborn + Pandas with secondary_y?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:17:02

4K+ Views

To get rid of grid lines when plotting with Pandas with secondary_y, we can take the following steps −Create a data frame using DataFrame wth keys column1 and column2.Use data frame data to plot the data frame. To get rid of gridlines, use grid=False.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = pd.DataFrame({"column1": [4, 6, 7, 1, 8], "column2": [1, 5, 7, 8, 1]}) data.plot(secondary_y=[5], grid=False) plt.show()Output

How to get the list of axes for a figure in Pyplot?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:18:35

5K+ Views

To get a list of axes of a figure, we will first create a figure and then, use get_axes() method to get the axes and set the labels of those axes.Create xs and ys using numpy and fig using figure() method. Create a new figure, or activate an existing figure.Use add_subplot() method. Add an '~.axes.Axes' to the figure as part of a subplot arrangement, where nrows=1, ncols=1 and index=1.. Get the axes of the fig, and set the xlabel and ylabel.Plot x and y data points with red color.To display the figure, use show() method.Exampleimport numpy as np from matplotlib ... Read More

Circular (polar) histogram in Python

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:03:31

2K+ Views

To plot circular (polar) histogram in Python, we can take the following steps−Create data points for theta, radii and width using numpy.Add a subplot to the current figure, where projection='polar' and nrows=1, ncols=1 and index=1.. Make a bar plot using bar() method, with theta, radii and width data pointsIterate radii and bars after zipping them together and set the face color of the bar and the alpha value. Lesser the alpha value, greater the transparency.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True N = 20 theta = ... Read More

How to add different graphs (as an inset) in another Python graph?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:06:12

1K+ Views

To add different graphs (as an inset) in another Python graph, we can take the following steps −Create x and y data points using numpy.Using subplots() method, create a figure and a set of subplots, i.e., fig and ax.To create a new axis, add axis to the existing figure (Step 2).Plot x and y on the axis (Step 2).Plot x and y on the new axis (Step 3).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.linspace(-1, 1, 100) y = np.sin(x) fig, ax = plt.subplots() left, bottom, width, height = [.30, 0.6, ... Read More

Advertisements