Found 33676 Articles for Programming

Plotting a transparent histogram with non-transparent edge in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:45:49

1K+ Views

To plot a transparent histogram with non-transparent edge, we can take the following steps−Create a set of random data points (y).Initialize the number of bins to be drawn.To plot the histogram, we can use hist() method with edge color and facecolor tuplesTo 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 y = np.random.rand(100) nbins = 5 plt.hist(y, bins=nbins, edgecolor=(1, 0, 0, 1), lw=5, facecolor=(.09, .12, .65, .87), rwidth=0.8) plt.show()Output

How to share secondary Y-axis between subplots in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:42:32

4K+ Views

To share secondary Y-axis between subplots in matplotlib, we can take the following steps −Create x for data points.Add a subplot to the current figure, with nrows=2, ncols=1, at index=1 (ax0)Using twinx() method, create a twin of axes with a shared X-axis but independent Y-axis (ax1).Add a subplot to the current figure, with nrows=2, ncols=1 at index=2 (ax2)Using twinx() method, create a twin of axes with a shared X-axis but independent Y-axis (ax3).Using get_shared_y_axes() method, return a reference to the shared axes Grouper object for Yaxis.Create curves c1,  c2,  c3 and c4 with different colors, x and y data points.To move the legend box ... Read More

Line plot with arrows in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:44:09

5K+ Views

To plot with arrows in matplotlib, we can use arrow() method.StepsCreate x and y data points using numpy.Plot x and y with color=red and linewidth = 1.Use arrow method to add an arrow to the axes. The first two values in the arguments are the coordinates of the arrow base and the next two values are for the length of the arrow along X and Y direction.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 x = np.linspace(-2, 2, 100) y = np.sin(x) plt.plot(x, y, c='b', ... Read More

How to center an annotation horizontally over a point in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:43:18

1K+ Views

To center an annotation horizontally over a point, we can take the following steps−Create points for x and y using numpy.Create labels using xpoints.Use scatter() method to scatter the points.Iterate labels, xpoints and ypoints and annotate the plot with label, x and y with different properties, make horizontal alignment ha=center.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 xpoints = np.linspace(1, 10, 10) ypoints = np.random.rand(10) labels = ["%.2f" % i for i in xpoints] plt.scatter(xpoints, ypoints, c=xpoints) for label, x, y in zip(labels, xpoints, ypoints): ... Read More

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

Rotate xtick labels in Seaborn boxplot using Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 14:01:12

11K+ Views

To rotate xtick labels in Seaborn boxplot, we can take the following steps −Create data points for xticks.Draw a boxplot using boxplot() method that returns the axis.Now, set the xticks using set_xticks() method, pass xticks.Set xticklabels and pass a list of labels and rotate them by passing rotation=45, using set_xticklabels() method.To display the figure, use show() method.Exampleimport seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xticks = [1, 4, 5, 2, 3] ax = sns.boxplot(xticks) ax.set_xticks(xticks) ax.set_xticklabels(["one", "two", "three", "four", "five"], rotation=45) plt.show()OutputRead More

Extract Matplotlib colormap in hex-format

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:59:29

2K+ Views

To extract matplotlib colormap in hex-format, we can take the following steps −Get the rainbow color map.Iterate in the range of rainbow colormap length.Using rgb2hex method, convert rgba tuple to a hexadecimal representation of a color.Examplefrom matplotlib import cm import matplotlib cmap = cm.rainbow for i in range(cmap.N):    rgba = cmap(i)    print("Hexadecimal representation of rgba:{} is {}".format(rgba, matplotlib.colors.rgb2hex(rgba)))Output............... ........................ .................................... Hexadecimal representation of rgba:(1.0, 0.3954512068705424, 0.2018824091570102, 1.0) is #ff6533 Hexadecimal representation of rgba:(1.0, 0.38410574917192575, 0.1958454670071669, 1.0) is #ff6232 Hexadecimal representation of rgba:(1.0, 0.37270199199091436, 0.18980109344182594, 1.0) is #ff5f30 .........................................................

How do you directly overlay a scatter plot on top of a jpg image in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:59:04

2K+ Views

To directly overlay a scatter plot on top of a jpg image, we can take the following steps −Load an image "bird.jpg", using imread() method, Read an image from a file into an array.Now display data as an image.To plot scatter points on the image make lists for x_points and y_points.Generate random numbers for x and y and append in lists.Using scatter method, plot x and y points.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 data = plt.imread("logo2.jpg") im = plt.imshow(data) x_points = [] y_points ... Read More

Plot a histogram with Y-axis as percentage in Matplotlib

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

3K+ Views

To plot a histogram with Y-axis as percentage in matplotlib, we can take the following steps −Create a list of numbers as y.Create a number of bins.Plot a histogram using hist() method, where y, bins, and edgecolor are passed in the argument.Store the patches to set the percentage on Y-axis.Create a list of colors from the given alphanumeric numbers.To set the percentage, iterate the patches (obtained in step 3).Set the Y-axis ticks range.To display the figure, use show() method.Exampleimport random import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True y = [4, 1, 8, ... Read More

Setting Y-axis in Matplotlib using Pandas

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:56:41

6K+ Views

To set Y-Axis in matplotlib using Pandas, we can take the following steps −Create a dictionary with the keys, x and y.Create a data frame using Pandas.Plot data points using Pandas plot, with ylim(0, 25) and xlim(0, 15).To display the figure, use show() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True d = dict(    x=np.linspace(0, 10, 10),    y=np.linspace(0, 10, 10)*2 ) df = pd.DataFrame(d) df.plot(kind="bar", ylim=(0, 25), xlim=(0, 15)) plt.show()Output

Advertisements