Plot Sine Curve on Polar Axes Using Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:48:29

847 Views

To plot the sine curve on polar axes, 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() methodAdd an '~.axes.Axes' to the figure as part of a subplot arrangement.Get 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 fig = plt.figure() ax = fig.add_subplot(projection='polar') x = np.linspace(-5, 5, 100) y = ... Read More

Find Intersection of Two Line Segments in Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:48:02

7K+ Views

To find the intersection of two lines segments in Matplotlib and pass the horizontal and vertical lines through that point, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create two lines using slopes (m1, m2) and intercepts (c1 and c2). Initialize the slopes and intercept values.Create x data points using numpy.Plot x, m1, m2, c2 and c1 data points using plot() method.Using intercepts and slope values, find the point of intersection.Plot the horizontal and vertical lines with dotted linestyle.Plot xi and yi points on the plot.To display the figure, use ... Read More

Show Minor Tick Labels on a Log Scale with Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:47:30

8K+ Views

To show minor tick labels on a log-scale with 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.Plot x and y data points using plot() methodGet the current axis using gca() method.Set the yscale with log class by name.Change the appearance of ticks and tick label using ick_params() method.Set the minor axis formatter with format strings to format the tick.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import FormatStrFormatter plt.rcParams["figure.figsize"] = [7.50, 3.50] ... Read More

Hide Lines in Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:47:07

5K+ Views

To hide lines in Matplotlib, we can use line.remove() method.StepsSet the figure size and adjust the padding between and around the subplots.Create x, y1 and y2 data points using numpy.Make lines, i.e., line1 and line2, using plot() method.To hide the lines, use line.remove() method.Place a legend on the figure at the upper-right location.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(-10, 10, 100) y1 = np.sin(x) y2 = np.cos(x) line1, = plt.plot(x, y1, label="Line 1") line2, = plt.plot(x, y2, label="Line 2") ... Read More

Fill Area Under Step Curve Using Pyplot Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:45:41

12K+ Views

To fill the area under step curve using pyplot, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Ceate random data points, x, y1 and y2, using numpy.To fill the area under the curve, put x and y with ste="pre", using fill_between() method.Plot (x, y1) and (x, y2) lines using plot() method with drawstyle="steps" method.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(-10, 10, 100) y1 = np.sin(x) y2 = np.cos(x plt.fill_between(x, y1, step="pre", alpha=0.4) ... Read More

Boxplot with Variable Length Data in Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:45:10

681 Views

To make a boxplot with variable length data in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a list of data points.Make a box and whisker plot using boxplot() method.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 data = [[2, 4, 1, 3], [0, 4, 3, 2], [0, 0, 1, 0]] plt.boxplot(data) plt.show()Output

Save All Open Matplotlib Figures in One File

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:44:40

5K+ Views

To save all the open Matplotlib figures in one file at once, we can take follwong steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure (fig1) or activate an existing figure using figure() method.Plot the first line using plot() method.Create a new figure (fig2) or activate an existing figure using figure() method.Plot the Second line using plot() method.Initialize a variable, filename, to make a pdf file.Create a user-defind function, save_multi_image, and call it to save all the open matplotlib figures in one file at once. Create a new PdfPages object, pp.Get the ... Read More

Plot Collections Counter Histogram Using Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:44:11

4K+ Views

To plot a histogram, with collections.Counter, we can use bar() method. In bar() method, we can use collections.counter() to get the frequency for each element. Put the elements and their frequency as height.StepsSet the figure size and adjust the padding between and around the subplots.Make a list of a data points.Get the dictionary, d, using collections.Counter().Make bar plot with d.keys() and d.values().To display the figure, use show() method.Exampleimport collections from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = [0, 1, 2, 4, 1, 3, 0, 4, 1, 4, 3, 5, 6, 5, ... Read More

Show Title for Seaborn Pairplot or Pridgrid Diagram in Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:43:41

3K+ Views

To show the title for the diagram for Seaborn pairplot(), we can use pp.fig.suptitle() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe, i.e., a two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot pairwise relationships in a dataset.Add a centered title to the figure.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((5, 5)),                   columns=["a", "b", "c", "d", "e"]) pp = ... Read More

Check Pylab Backend of Matplotlib for Inline Running

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:43:04

390 Views

To check that pylab/pyplot backend of Matplotlib runs inline, we can use get_backend() method.The method returns the name of the current backend.Exampleimport matplotlib inline = matplotlib.get_backend() print("Backend: ", inline)OutputBackend: Qt5Agg

Advertisements