Programming Articles - Page 1162 of 3363

How do I get all the bars in a Matplotlib bar chart?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:00:45

462 Views

To get all the bars in a Matplotlib chart, we can use the bar() method and return the bars.−StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Create x and y data points using subplots() method.Make a bar plot and store it in bars variable.Set the facecolor of a particular set of bars.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() x = np.arange(7) y = np.random.rand(7) bars = ax.bar(x, ... Read More

How to remove a frame without removing the axes tick labels from a Matplotlib figure in Python?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:59:03

2K+ Views

To remove a frame without removing the axes tick labels from a Matplotlib figure, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a list of y data points.Plot the y data points using plot() methodTo remove the left-right-top and bottom spines, we can use set_visible() method.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 y = [0, 2, 1, 5, 1, 2, 0] plt.plot(y, color='red', lw=7) for pos in ['right', 'top', 'bottom', 'left']:    plt.gca().spines[pos].set_visible(False) plt.show()OutputRead More

How to get a reverse-order cumulative histogram in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:57:25

2K+ Views

To get a reverse-order cumulative histogram in Matplotlib, we can use cumulative = -1 in the hist() method.Set the figure size and adjust the padding between and around the subplots.Make a list of data points.Plot a histogram with data and cumulative = -1.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 = [1, 2, 2, 3, 1, 4, 3, 0, 1, 3, 0] plt.hist(data, edgecolor='black', align="mid", cumulative=-1) plt.show()Output

How to use an update function to animate a NetworkX graph in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:55:10

2K+ Views

To use an update function to animate a NetworkX graph 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.Initialize a graph with edges, name, and graph attributes.Add nodes to the graph using add_nodes_from() method.Draw the graph G with Matplotlib.Use FuncAnimation() class to make an animation by repeatedly calling a function, animate.Function animate clears the current figure, generate two random numbers, and draws the edges between them.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, ... Read More

How to plot a pcolor colorbar in a different subplot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:52:31

3K+ Views

To plot a pcolor colorbar in a different subplot in Matplotlib, 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 wuth two rows and two columns.Make a list of colormaps.Iterate the axes and create a pseudocolor plot with a non-regular rectangular grid.Make colorbars with the same axes of pcolormesh.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 fig, axs = plt.subplots(2, 2) cm = ['plasma', 'copper'] for col ... Read More

How to plot a smooth 2D color plot for z = f(x, y) in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:48:33

4K+ Views

To plot a smooth 2D color plot for z = f(x, y) 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.Get z data points using f(x, y).Display the data as an image, i.e., on a 2D regular raster, with z data points.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 def f(x, y):    return np.array([i * i + j * j for j in ... Read More

Display two Sympy plots as one Matplotlib plot (add the second plot to the first)

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:47:19

1K+ Views

To display two sympy plots as one Matplotlib plot, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Transform strings into instances of :class:'Symbol' class.Plot a function of a single variable as a curve.Use the extend method to add all the series of plot2 (p2) in plot1 (p1).To display the figure, use show() method.Examplefrom sympy import symbols from sympy.plotting import plot from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = symbols('x') p1 = plot(x*x, show=False) p2 = plot(x, show=False) p1.extend(p2) p1.show()OutputRead More

Number of pairs with maximum sum in C++

Aishwarya Naglot
Updated on 12-Nov-2024 10:33:30

527 Views

We need to find a number of all the pairs that has the maximum sum, In a given array of integers. A pair consists of two numbers, and the sum is simply the result of adding them. We will see multiple solutions to this problem, starting with a naive (brute force) approach and moving to more optimized solutions. Below is the list of approaches we will cover in this article: Brute Force approach using two loops Max Sum Pairs with Sorting ... Read More

Number of pairs with Bitwise OR as Odd number in C++

Hafeezul Kareem
Updated on 26-Oct-2021 19:18:54

273 Views

Given an array, we have to find the number of pairs whose Bitwise OR is an odd number. Let's see the example.Inputarr = [1, 2]Output1 There is only one pair whose Bitwise OR is an odd number. And the pair is (1, 2).AlgorithmInitialise the array with random numbers.Initialise the count to 0.Write two loops to get the pairs of the array.Compute the bitwise OR between every pair.Increment the count if the result is an odd number.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getOddPairsCount(int arr[], int n) {    int count ... Read More

Number of pairs whose sum is a power of 2 in C++

Hafeezul Kareem
Updated on 26-Oct-2021 19:09:44

936 Views

Given an array, we have to find the number of pairs whose sum is a power of 2. Let's see the example.Inputarr = [1, 2, 3]Output1 There is only one pair whose sum is a power of 2. And the pair is (1, 3).AlgorithmInitialise array with random numbers.Initialise the count to 0.Write two loops to get all the pairs of the array.Compute the sum of every pair.Check whether the sum is a power of 2 or not using bitwise AND.Increment the count if the count is a power of 2.Return the count.ImplementationFollowing is the implementation of the above algorithm in ... Read More

Advertisements