Plot a 2D Histogram in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:38:46

3K+ Views

To plot a 2D histogram in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots.Plot x and y using hist2d() method.Set the title of the plot.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 = 2 * np.random.randn(5000) y = x + np.random.randn(5000) fig, ax = plt.subplots() _ = ax.hist2d(x[::10], y[::10]) ax.set_title('2D Histogram') plt.show()OutputRead More

Make Joint Bivariate Distributions in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:37:49

314 Views

To make joint bivariate distributions in matplotlib, we can use the scatter method.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots.Plot x and y using scatter() method.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 = 2 * np.random.randn(5000) y = x + np.random.randn(5000) fig, ax = plt.subplots() _ = ax.scatter(x, y, alpha=0.08, cmap="copper", c=x) plt.show()OutputRead More

Align Bar and Line in Matplotlib Two Y-Axes Chart

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:36:31

2K+ Views

To align the bar and line in matplotlib two Y-axes chart, we can use twinx() method to create a twin of Axes with a shared X-axis but independent Y-axis.StepsSet the figure size and adjust the padding between and around the subplots.Make a Pandas dataframe with columns 1 and 2.Plot the dataframe using plot() method with kind="bar", i.e., class by name.Use twinx() method to create a twin of Axes with a shared X-axis but independent Y-axis.Plot the axis (Step 3) ticks and dataframe columns values to plot the lines.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import ... Read More

Draw a Border Around Subplots in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:35:14

5K+ Views

To draw a border around subplots in matplotlib, we can use a Rectangle patch on the subplots.StepsSet the figure size and adjust the padding between and around the subplots.Add a subplot to the current figure using subplot(121).Get the subplot axes.Add a rectangle defined via an anchor point *xy* and its *width* and *height*.Add a rectangle patch to the current subplot based on axis (Step 4).Set whether the artist uses clipping.Add a subplot to the current figure using subplot(122).Set the title of the current subplot.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] ... Read More

Plotting Cumulative Graph of Python Datetimes in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:32:12

652 Views

To plot a cumulative graph of python datetimes, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a Pandas dataframe with some college data, where one key for time difference and another key for number students have admissioned in the subsequent year.Plot the dataframe using plot() method where kind='bar', i.e., class by name.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True college_student_data = {'durations': [1, 2, 2.5, 3, 4.5, 5, 5.5, 6, 6.5, 7], ... Read More

Select Specific Subplot in Matplotlib Programmatically

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:30:41

762 Views

To select a specific subplot in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure() method.Iterate in a range, i.e., number subplots to be placed.In the loop itself, add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.Now, select an axes plot line with red color.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 fig = plt.figure() for index ... Read More

Smooth Interpolation with pcolormesh in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:29:34

2K+ Views

To get smooth interpolation when using pcolormesh, we can use shading="gouraud" class by name.StepsSet the figure size and adjust the padding between and around the subplots.Create data, x and y using numpy meshgrid.Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.To display the figure, use show() method.Exampleimport matplotlib.pylab as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.random((3, 3)) x = np.arange(0, 3, 1) y = np.arange(0, 3, 1) x, y = np.meshgrid(x, y) plt.pcolormesh(x, y, data, cmap='RdBu', shading='gouraud') plt.show()OutputRead More

Change Axes Background Color in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:28:29

5K+ Views

To change the axes background color, we can use set_facecolor() method.StepsSet the figure size and adjust the padding between and around the subplots.Get the current axes using gca() method.Set the facecolor of the axes.Create x and y data points using numpy.Plot x and y data points using plot() method.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 ax = plt.gca() ax.set_facecolor("orange") x = np.linspace(-2, 2, 10) y = np.exp(-x) plt.plot(x, y, color='red') plt.show()OutputRead More

Populating Matplotlib Subplots Through a Loop and a Function

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:27:19

6K+ Views

To populate matplotlib subplots through a loop and a function, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots with number of rows = 3 and number of columns = 2.Make a function to iterate the columns of each row and plot the x data points using plot() method at each column index.Iterate rows (Step 2) and create random x data points and call iterate_columns() function (Step 3).To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] ... Read More

Hide Colorbar of a Seaborn Heatmap

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:25:23

9K+ Views

To hide the colorbar of a Seaborn heatmap, we can use cbar=False in heatmap() method.StepsSet the figure size and adjust the padding between and around the subplots.Make a dataframe using 4 columns.Use heatmap() method to plot rectangular data as a color-encoded matrix.To display the figure, use show() method.Exampleimport seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(np.random.random((4, 4)), columns=["a", "b", "c", "d"]) sns.heatmap(df, cbar=False) plt.show()Output

Advertisements