Plot Lines with Colors Using Colormap in Matplotlib

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

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

Add Group Labels for Bar Charts in Matplotlib

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

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

Rotate XTickLabels in Matplotlib for Equal Spacing

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

394 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

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

Add Caption Below X-Axis for Scatter Plot Using Matplotlib

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

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

Plot Confusion Matrix with String Axis in Python

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

634 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

Hide Axes in Matplotlib 3D

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

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

Set Variable Point Size in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:53:39

440 Views

To set the variable point size in matplotlib, we can take the following steps−Initialize the coordinates of the point.Make a variable to store the point size.Plot the point using scatter method, with marker=o, color=red, s=point_size.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 xy = (3, 4) point_size = 100 plt.scatter(x=xy[0], y=xy[1], marker='o', c='red', s=point_size) plt.show()Output

Draw Rectangle Over Specific Region in Matplotlib Graph

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:52:30

768 Views

To draw a rectangle over a specific region in a matplotlib graph, we can take the following steps −Using subplots() method, create a figure and a set of subplots, where nrows=1.Using rectangle, we can create a rectangle, defined via an anchor point and its width and height. Where, edgecolor=orange, linewidth=7, and facecolor=green.To plot a diagram over the axis, we can create a line using plot() method, where line color is red.Add a rectangle patch on the diagram, using add_patch() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True figure, ax = plt.subplots(1) ... Read More

Create a Draggable Legend in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:52:08

2K+ Views

To create a draggable legend in matplotlib, we can take the following steps −Create two lines, line1 and line2, using plot() method.Place the legend for plot line1 and line2 with ordered lables at location 1, using legend() method.To create a draggable legend, use set_draggable() method, where state=True. If state=False, then we can't drag the legend.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 line1, = plt.plot([1, 2, 3]) line2, = plt.plot([3, 2, 1]) leg = plt.legend([line2, line1], ["line 2", "line 1"], loc=1) leg.set_draggable(state=True) plt.show()OutputOn the output window, you can drag the legend around with ... Read More

Advertisements