Found 1034 Articles for Matplotlib

How to change the order of plots in Pandas hist command?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:05:53

360 Views

To change order of plots in Pandas hist commad, we can take the following steps −Make a data frame using Pandas.Plot a histogram with the data frame.Plot the data frame in different order.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'a': [1, 1, 1, 1, 3],    'b': [1, 1, 2, 1, 3],    'c': [2, 2, 2, 1, 3], }) df.hist() df[['c']].hist() df[['a']].hist() df[['b']].hist() plt.show()Output

How to put text outside Python plots?

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

3K+ Views

To put text outside a plot, we can change the text position by changing the value of text_pos_x and text_pos_yStepsCreate data points for x and y.Initialize the text position of x and y.To plot x and y, use plot() method with color='red'.Use text() method to add text to figure.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, 5, 100) y = np.exp(x) text_pos_x = 0.60 text_pos_y = 0.50 plt.plot(x, y, c='red') plt.text(text_pos_x, text_pos_y, "$\mathit{y}=e^{x}$", fontsize=14, transform=plt.gcf().transFigure, color='green') plt.show()OutputRead More

How to add group labels for bar charts in Matplotlib?

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

853 Views

To make grouped labels for bar charts, we can take the following steps −Create lists for labels, men_means and women_means with different data elements.Return evenly spaced values within a given interval, using numpy.arrange() method.Set the width variable, i.e., width=0.35.Create fig and ax variables using subplots method, where default nrows and ncols are 1.The bars are positioned at *x* with the given *align*\ment. Their dimensions are given by *height* and *width*. The vertical baseline is *bottom* (default 0), so create rect1 and rect2 using plt.bar() method.Set the Y-Axis label using plt.ylabel() method.Set a title for the axes using set_title() method.Get or set the current tick locations and ... Read More

How to rotate xticklabels in Matplotlib so that the spacing between each xticklabel is equal?

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

349 Views

To rotate xticklabels in matplotlib to make equal spacing between two xticklabels, we can take the following steps −Make a list of numbers from 1 to 4.Using subplot(), sdd a subplot to the current figure.Add xticks and yticks on the current subplot (using step 1).Set xtick labels by passing a list and to make label rotation (= 45).To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot() ax1.set_xticks(x) ax1.set_yticks(x) ax1.set_xticklabels(["one", "two", "three", "four"], rotation=45) plt.show()OutputRead More

Colorplot of 2D array in Matplotlib

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

994 Views

To plot a colorplot of a 2D array, we can take the following steps −Create data (i.e., 2D array) using numpy.For colorplot, use imshow() method, with input data (Step 1) and colormap is "PuBuGn".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 = np.random.rand(4, 4) plt.imshow(data, cmap='PuBuGn') plt.show()Output

Adjusting gridlines and ticks in Matplotlib imshow

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

3K+ Views

To adjust gridlines and ticks in matplotlib imshow(), we can take the following steps−Create data, a 2D array, using numpy.Using imshow() method, display data as an image.Set xticks and yticks using set_xticks and set_yticks method.To set the xticklabels and yticklabels, use set_xticklabels and set_yticklabels method.Lay out a grid in current line style. Supply the list of x an y positions using grid() 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 data = np.random.rand(9, 9) plt.imshow(data, interpolation="nearest") ax = plt.gca() ax.set_xticks(np.arange(-.5, 9, 1)) ax.set_yticks(np.arange(-.5, 9, 1)) ... Read More

Matplotlib Plot Lines with Colors through Colormap

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

12K+ Views

To plot lines with colors through colormap, we can take the following steps−Create x and y data points using numpyPlot x and y data points using plot() method.Count n finds, number of color lines has to be plotted.Iterate in a range (n) and plot the lines.Limit the x ticks range.Use show() method to display the figure.Exampleimport numpy as np import matplotlib.pylab as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 2 * np.pi, 64) y = np.exp(x) plt.plot(x, y) n = 20 colors = plt.cm.rainbow(np.linspace(0, 1, n)) for i in range(n): plt.plot(x, i * y, color=colors[i]) plt.xlim(4, ... Read More

How to plot a confusion matrix with string axis rather than integer in Python?

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:54:44

506 Views

To plot a confusion matrix with string axis rather than integer in Python, we can take the following steps−Make a list for labels.Create a confusion matrix. Use confusion_matrix() to calculate accuracy of classification.3. Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Plot the values of a 2D matrix or array as a color-coded image.Using colorbar() method, create a colorbar for a ScalarMappable instance, *mappable*6. Set x and y ticklabels using set_xticklabels and set_yticklabels methods.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt from sklearn.metrics import confusion_matrix plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True labels ... Read More

How can I hide the axes in Matplotlib 3D?

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:54:18

1K+ Views

To hide the axes in matplotlib 3D, we can take the following steps−Create a 2D array, where x, y, z, u, v and w are the coordinates of the arrow locations and direction components of the arrow vectors.Using figure() method, create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement, using add_subplot() methodPlot a 3D field of arrows, using quiver() method.Using ylim, xlim, zlim, limit the range of the axesSet the title of the plot.Create two axes (ax1 and ax2). Set the titles "With Axes" and "Without Axes". Using set_axis_off() ... Read More

Adding caption below X-axis for a scatter plot using Matplotlib

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

4K+ Views

To add caption below X-axis for a scatter plot, we can use text() method for the current figure.StepsCreate x and y data points using numpy.Create a new figure or activate an existing figure using figure() method.Plot the scatter points with x and y data points.To add caption to the figure, use text() method.Adjust the padding between and around the subplots.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.random.rand(10) y = np.random.rand(10) fig = plt.figure() plt.scatter(x, y, c=y) fig.text(.5, .0001, "Scatter Plot", ha='center') plt.tight_layout() plt.show()OutputRead More

Advertisements