Plotting Upper and Lower Triangle of a Heatmap in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:43:04

1K+ Views

To plot only the upper/lower triangle of a heatmap in matplotlib, we can use numpy to get the masked 2D array and convert them into an image to produce a heatmap.StepsSet the figure size and adjust the padding between and around the subplots.Create a random data of 5×5 dimension.Use numpy.tri() method to create an array with 1's at and below the given diagonal and 0's elsewhere.Get the masked 2D array data with masked array (Using step 3).Use imshow() method to display the data as an image, i.e., on a 2D regular raster.To display the figure, use show() method.Exampleimport numpy as ... Read More

Control Matplotlib Marker Orientation

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:42:45

629 Views

To control matplotlib marker orientation, we can use marker tuple that contains a number of sides, style and rotation or orientation of the marker.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Make an array of 10 different rotations.Zip x, y and i. Iterate them and plot the points using plot() method with marker tuple.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.rand(10) y = np.random.rand(10) i = np.linspace(0, 10, 10) for x, ... Read More

Plot Emoji as Label for Bar in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:41:01

2K+ Views

We can use annotate() to place an emoji at the top of a bar.StepsSet the figure size and adjust the padding between and around the subplots.Make a list of frequencies and labels conatining emojis.Create a new figure or activate an existing figure using figure() method.Plot bars using bar() method.Use annotate() method to place emojis as a labelTo display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True freqs = [7, 8, 5, 3, 6] labels = ['😊', '😲', '😂', '😃', '😛'] plt.figure() p1 = plt.bar(np.arange(len(labels)), freqs) for rect1, ... Read More

Add Border to Line in Matplotlib Plot Function

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:40:45

2K+ Views

To give a border to a line in matplotlib plot function, we can call plot() function twice with varying line width.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points where line width=10 and color=black.Again plot x and y points where line width=8 and color=red.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 100) y = np.sin(x) plt.plot(x, y, c='black', lw=10) plt.plot(x, y, c='red', lw=8) plt.show()OutputRead More

Change Linewidth of a Hatch in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:40:06

3K+ Views

To change the linewidth of a hatch in matplotlib, we can set the linewidth of the hatch in the params.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y=sin(x) data points using numpy.Set the linewidth of the hatch in the plot.Plot x and y data points using scatter() method with a square marker having "/" hatches with set linewidth.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-5, 5, 25) y = np.sin(x) plt.rcParams['hatch.linewidth'] = 1 plt.scatter(x, y, ... Read More

Scatter Points on 3D Plot without Axes and Grids in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:36:01

872 Views

To plot scatter points on a 3D plot without axes in matplotlib, we can use scatter() method and make the axes OFF.StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure() method.Add an axis as a subplot arrangement.Create xs, ys and zs data points using numpy.Use scatter() method to create a scatter plot.Use ax.axis('off') method to hide the axes.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(projection="3d") ... Read More

Add Custom Border to Certain Cells in Matplotlib Seaborn Plot

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:35:48

1K+ Views

To add a custom border to certain cells in a Matplotlib/Seaborn plot.StepsSet the figure size and adjust the padding between and around the subplots.Create a dataframe with some columns.Plot a matrix dataset as a hierarchically-clustered heatmap.Get heatmap axis as a subplot arrangement.To add a custom border to certain cells in Matplotlib, we can intialize a variable, border_color.Using custom bordder color, add a rectangle patch on the heatmap axes.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({"col1": [1, 4, 2, ... Read More

Show Text Label in a Plot Legend in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:34:52

1K+ Views

To just show the text label in plot legend we can use legend method with handlelength=0, handletextpad=0 and fancybox=0 in the arguments.StepsSet the figure size and adjust the padding between and around the subplots.Create random x and y data points using numpy.Create a figure and a set of subplots using subplots() method.Plot x and y data points using plot() method with label "Zig-Zag" for the legend.Use legend() method to place the label for the plot with handlelength=0, handletextpad=0 and fancybox=0 in the arguments.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = ... Read More

Box Plot with Min, Max, Average and Standard Deviation in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:34:20

6K+ Views

To make a box plot for min, max, average and standard deviation in matplotlib, StepsSet the figure size and adjust the padding between and around the subplots.Create a random dataset of 5☓5 dimension.Find min, max, average and standard deviation from the data.Make a Pandas dataframe with Step 3, min, max, average and standard deviation data.Make a box plot from the dataframe column.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randn(5, 5) min = data.min(0) max = data.max(0) avg = data.mean(0) std = data.std(0) df = ... Read More

Add Scatter Points to Boxplot using Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:33:17

5K+ Views

To add a scatter of points to a boxplot using matplotlib, we can use boxplot() method and enumerate the Pandas dataframe to get the x and y data points to plot the scatter points.StepsSet the figure size and adjust the padding between and around the subplots.Make a dataframe using DataFrame class with the keys, Box1 and Box2.Make boxplots from the dataframe.Find x and y for the scatter plot using data (Step 1).To display the figure, use show() method.Exampleimport pandas as pd import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = ... Read More

Advertisements