Plot Black and White Binary Map in Matplotlib

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

6K+ Views

To plot black-and-white binary map in matplotlib, we can create and add two subplots to the current figure using subplot() method, where nrows=1 and ncols=2. To display the data as a binary map, we can use greys colormap in imshow() method.StepsCreate data using numpyAdd two sublots, nrows=1 and ncols=2. Consider index 1.To show colored image, use imshow() method.Add title to the colored map.Add a second subplot at index 2.To show the binary map, use show() method with Greys colormap.To adjust the padding between and around the subplots, use tight_layout() method.To display the figure, use show() method.Exampleimport numpy as np from ... Read More

Create a Legend for a Contour Plot in Matplotlib

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

3K+ Views

To create a legend for a contour plot in matplotlib, we can take the following steps−Create x, y and z data points to plot the contour function.To create a 3D filled contour plot, we can use contourf() method with x, y, z and different levels.Make a list of rectangle with the returned contour signature collection and set face colorNow, place the legend in the plot using proxy (of step 3) and different labels.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, y = np.meshgrid(np.arange(10), np.arange(10)) ... Read More

Change Text Color of Font in Legend using Matplotlib

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

6K+ Views

To change the text color of font in the legend in matplotlib, we can take the following steps−Create x and y data points using numpy.Plot x and y using plot() method, where color of line is red and label is "y=exp(x)".To place the legend, use legend() method with location of the legend and store the returned value to set the color of the text.To set the color of the text, use set_color() method with green color.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 = ... Read More

Overlapping Y-Axis and X-Axis Tick Labels in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:11:58

4K+ Views

To reduce the chances of overlapping between x and y tick labels in matplotlib, we can take the following steps −Create x and y data points using numpy.Add a subplot to the current figure at index 1 (nrows=1 and ncols=2).Set x and y margins to 0.Plot x and y data points and add a title to this subplot, i.e., "Overlapping".Add a subplot to the current figure at index 2 (nrows=1 and ncols=2).Set x and y margins to 0.Plot x and y data points and add a title to this subplot, i.e., "Non Overlapping".The objective of MaxNLocator and prune ="lower" is that the smallest tick will be removed.To display the figure, ... Read More

Plot Scatter Points with Increasing Size of Marker in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:11:20

781 Views

To plot scatter points with increasing size of marker, we can take the following steps−StepsCreate x and y data pointsTo get increasing size of marker, make a list of numbers.Use scatter method to plot scatter points.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 x = [0, 2, 4, 6, 8, 10] y = [0] * len(x) s = [10 * 4 ** n for n in range(len(x))] plt.scatter(x, y, s=s, c='red') plt.show()Output

Style Part of Label in Legend in Matplotlib

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

258 Views

To style a part of label in legend, we can take the following steps −Create data point for x using numpy.Plot a sine curve using np.sin(x) with a text label.Plot a cosine curve using np.cos(x) with a text label.To place the legend on the plot, use legend() 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 x = np.linspace(-1, 1, 10) plt.plot(x, np.sin(x), label="This is $\it{a\ sine\ curve}$") plt.plot(x, np.cos(x), label="This is $\bf{a\ cosine\ curve}$") plt.legend(loc='lower right') plt.show()OutputRead More

Plot Only a Table in Matplotlib

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

6K+ Views

To plot only a table, we can take the following steps−Create fig and axs, using subplots. Create a figure and a set of subplots.Create random data for 10 rows and 3 columns.Create a tuple for columns name.axis('tight') − Set the limits, just large enough to show all the data, then disable further autoscaling.axis('off') − Turn off axis lines and labels. Same as ''False''.To add a table on the axis, use table() instance, with column text, column labels, columns, and location=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"] ... Read More

Add Different Graphs as 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

Change Order of Plots in Pandas hist Command

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

604 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

Add Vertical Lines to a Distribution Plot in Matplotlib

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

3K+ Views

To add vertical lines to a distribution plot, we can take the following steps−Create a list of numbers.Create an axis using sns.displot().Get x and y data of the axis ax.Plot a vertical line on the plot.Remove the line at the 0th index.To display the figure, use show() method.Exampleimport seaborn as sns, numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [5, 6, 7, 2, 3, 4, 1, 8, 2] ax = sns.distplot(x, kde=True) x = ax.lines[0].get_xdata() y = ax.lines[0].get_ydata() plt.axvline(x[np.argmax(y)], color='red') ax.lines[0].remove() plt.show()OutputRead More

Advertisements