Found 1034 Articles for Matplotlib

Remove the X-axis ticks while keeping the grids (Matplotlib)

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

2K+ Views

To remove the X-ticks while keeping the grids, we can take the following steps−Use gca() method to get the current axes, creating one if necessary.Plot the x and np.sin(x) using plot() method with linewidth=5, label y=sin(x).Remove yticks and xticks by passing empty array in the argument of set_xticklabels and set_yticklabels methods respectively.Configure grid lines by putting flag as True.Place the legend for the plot label in the argument.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 2*np.pi, 100) ax = plt.gca() ax.plot(x, np.sin(x), c='r', lw=5, ... Read More

How to make a log histogram in Python?

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

3K+ Views

To make a log histogram, we can use log=True in the argument of the hist() method.StepsMake a list of numbers.Plot a histogram with density=True.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 k = np.array([5, 5, 5, 5]) x, bins, p = plt.hist(np.log(k), density=True, log=True) plt.show()Output

How to shade the regions between the curves in Matplotlib?

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

6K+ Views

To shade the regions between curves, we can use the fill_between() method.StepsInitialize the variable n. Initiliize x and y data points using numpy.Create a figure and a set of subplots, fig and ax.Plot the curve using plot method.Use fill_between() method, fill the area between the two curves.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 = 256 X = np.linspace(-np.pi, np.pi, n, endpoint=True) Y = np.sin(2 * X) fig, ax = plt.subplots() ax.plot(X, Y, color='blue', alpha=1.0) ax.fill_between(X, 0, Y, color='blue', alpha=.2) plt.show()OutputRead More

Plotting a transparent histogram with non-transparent edge in Matplotlib

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

822 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

3K+ 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

4K+ 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

902 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

865 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

10K+ 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

1K+ 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 .........................................................

Advertisements