Found 1034 Articles for Matplotlib

How to independently set horizontal and vertical, major and minor gridlines of a plot?

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:54:15

845 Views

To set horizontal and vertical, major and minor grid lines of a plot, we can use grid() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make horizontal grid lines for major ticks.Locate minor locator on the axes.Use grid() method to make minor grid lines.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from matplotlib.ticker import MultipleLocator plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() ax.yaxis.grid(which="major", color='r', linestyle='-', linewidth=2) ml = MultipleLocator(0.10) ax.xaxis.set_minor_locator(ml) ax.xaxis.grid(which="minor", color='k', linestyle='-.', linewidth=0.7) plt.show()OutputRead More

Contour hatching in Matplotlib plot

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:53:39

623 Views

To plot contour with hatching, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x, y and z data points using numpy.Flat the x and y data points.Create a figure and a set of subplots.Plot a contour with different hatches.Create a colorbar for a scalar mappable instance.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-3, 5, 150).reshape(1, -1) y = np.linspace(-3, 5, 120).reshape(-1, 1) z = np.cos(x) + np.sin(y) x, y = ... Read More

How can I move a tick label without moving corresponding tick in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:53:06

487 Views

To move a tick label without moving corresponding tick in Matplotlib, we can use axvline() method and can annotate it accordingly.StepsSet the figure size and adjust the padding between and around the subplots.Initialize a variable, delta.Create x and y data points using numpy.Plot delta using axvline() methodAnnotate that line using annotate() method.Plot x and y data points using plot() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True delta = 2.0 x = np.linspace(-10, 10, 100) y = np.sinc(x - delta) plt.axvline(delta, ls="--", ... Read More

How to access axis label object in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:52:12

2K+ Views

To axes axis label object in Matplotlib, we can use ax.xaxis.get_label().get_text() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Initialize a variable, N, for number samples.Create random data points using numpy.Plot x data points using plot() method.Set X-axis label using set_xlabel() method.To get the xlabel, use get_label() method and get_text() 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 fig, ax = plt.subplots() N = 100 x = np.random.rand(N) ax.plot(x) ax.set_xlabel("X-axis") x_lab = ax.xaxis.get_label() print("Label is: ... Read More

Adjust one subplot's height in absolute way (not relative) in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:51:13

219 Views

To adjust one subplot's height in absolute way 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.For absolute height of subplot, use Axes() classAdd an axes to the figure.Plot the data points on the axes.To display the figure, use show() method.Examplefrom matplotlib import pyplot as pl pl.rcParams["figure.figsize"] = [7.50, 4.50] pl.rcParams["figure.autolayout"] = True figure = pl.figure() axes = pl.Axes(figure, [.4, .6, .25, .25]) figure.add_axes(axes) pl.plot([1, 2, 3, 4], [1, 2, 3, 4]) axes = pl.Axes(figure, [.4, ... Read More

Calculate the curl of a vector field in Python and plot it with Matplotlib

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:50:13

1K+ Views

To calculate the curl of a vector field in Python and plot in with Matplotlib, we can use quiver() method and calculate the corresponding data.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 a 3D axes to the figure as part of a subplot arrangement.Create x, y and z data points using numpy meshgrid.Create u, v and w data curl vector positions.Use quiver() method to get vectors.Turn off the axes.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] ... Read More

How to assign specific colors to specific cells in a Matplotlib table?

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:28:40

2K+ Views

To assign specific colors to specific cells in a Matplotlib table, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a tuple for columns attribute.Make a list of lists, i.e., list of records.Make a list of lists, i.e., color of each cell.Create a figure and a set of subplots.Add a table to an axes ax.Turn off the axes.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True columns = ('name', 'age', 'marks', 'salary') cell_text = [["John", "23", "98", "234"], ["James", ... Read More

How to plot with different scales in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:26:48

5K+ Views

To plot with different scales in matplotlib, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Create t, data1 and data2 data points using numpyCreate a figure and a set of subplots, ax1.Initialize a color variable.Set x and y labels of axis 1.Plot t and data1 using plot() method.Set label colors using tick_params() method.Create a twin Axes sharing the X-axis, ax2.Perform steps 4, 6, 7 with a different dataset on axis 2.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"] ... Read More

How can you clear a Matplotlib textbox that was previously drawn?

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:47:43

2K+ Views

To clear a Matplotlib textbox that was previously drawn, 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.Plot x and y using plot() method.Place characters token on the plot.To clear the text, use text.remove(), where text is a returned artist.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, ax = plt.subplots() x = np.linspace(-10, 10, 100) y = np.sin(x) ax.plot(x, y) text = fig.text(0.5, 0.96, ... Read More

Horizontal stacked bar chart in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:25:28

9K+ Views

To plot stacked bar chart in Matplotlib, we can use barh() methodsStepsSet the figure size and adjust the padding between and around the subplots.Create a list of years, issues_addressed and issues_pending, in accordance with years.Plot horizontal bars with years and issues_addressed data.To make stacked horizontal bars, use barh() method with years, issues_pending and issues_addressed dataPlace the legend on the plot.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True year = [2014, 2015, 2016, 2017, 2018, 2019] issues_addressed = [10, 14, 0, 10, 15, 15] issues_pending = [5, 10, 50, ... Read More

Advertisements