Programming Articles - Page 1252 of 3363

How to hide axes and gridlines in Matplotlib?

Rishikesh Kumar Rishi
Updated on 12-Sep-2023 01:31:20

45K+ Views

To hide axes (X and Y) and gridlines, we can take the following steps −Create x and y points using numpy.Plot a horizontal line (y=0) for X-Axis, using the plot() method with linestyle, labels.Plot x and y points using the plot() method with linestyle, labels.To hide the grid, use plt.grid(False).To hide the axes, use plt.axis('off')To activate the labels' legend, use the legend() method.To display the figure, use the 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(-10, 10, 50) y = np.sin(x) plt.axhline(y=0, c="green", linestyle="dashdot", label="y=0") plt.plot(x, y, c="red", lw=5, linestyle="dashdot", label="y=sin(x)") plt.grid(False) plt.axis('off') ... Read More

How to plot matplotlib contour?

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

383 Views

To plot matplotlib contour, we can take the following steps −Create data points for x, y and h using numpy.Using the countourf() method, create a colored 3D (alike) plot.Using the set_over() method, set the color for high out-of-range values when "norm.clip = False".Using the set_under() method, set the color for low out-of-range values when "norm.clip = False".Using the changed() method, call this whenever the mappable is changed to notify all the callbackSM listeners to the "changed" signal.Use the show() method to display the figure.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = ... Read More

How to display a 3D plot of a 3D array isosurface in matplotlib mplot3D or similar?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:31:20

1K+ Views

Let's take an example to see how to display a 3D plot of a 3D array isosurface in matplotlib −Exampleimport numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(-5, 5, 0.25) y = np.arange(-5, 5, 0.25) x, y = np.meshgrid(x, y) h = x ** 2 + y ** 2 fig = plt.figure() ax = Axes3D(fig) ax.plot_surface(x, y, h, rstride=1, cstride=1, cmap=plt.cm.rainbow, linewidth=0, antialiased=False) plt.show()Output

How to save the plot to a numpy array in RGB format?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:29:13

1K+ Views

To save the plot to a numpy array in RGB format, we can take the following steps −Create r, g and b random array using numpy.Zip r, g and b (grom step 1) to make an rgb tuple list.Convert rgb into a numpy array to plot it.Plot the numpy array that is in rgb format.Save the figure at the current location.To display the figure, use the 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 r = np.random.rand(100) g = np.random.rand(100) b = np.random.rand(100) rgb = zip(r, g, b) arr = np.array([item for item in rgb]) plt.plot(arr) plt.savefig("myplot.png") ... Read More

Largest sum subarray with at-least k numbers in C++

Hafeezul Kareem
Updated on 09-Apr-2021 14:06:53

424 Views

Let's see the steps to complete the program.Initialise the array.Initialise max_sum array of size n.Find the max sum for every index and store it in max_sum array.Compute the sum of all the elements and store it in a variable sum.Write a loop that iterates from i = k to n.Add a[i] - a[i - k] to the sum.Update the result with max of result, sum.Update the result with max of result, sum + max_sum[i - k].ExampleLet's see the code. Live Demo#include using namespace std; int getMaxSum(int a[], int n, int k) {    int maxSum[n];    maxSum[0] = a[0];    int ... Read More

Largest subarray with equal number of 0s and 1s in C++

Hafeezul Kareem
Updated on 09-Apr-2021 14:14:28

221 Views

Let's see the steps to complete the program.Initialise the array.Make all zeroes in the array to -1.Have a map an empty map to store the previous indexes.Initialise sum to 0, max length to 0 and ending index to -1.Write a loop that iterates till n.Add current element to sum.If the sum is equal to 0.Update the max length with i + 1.And ending index to i.If the sum is present in previous sums map and i - previousIndexes[sum] is greater than max length.Update the max length and ending index.Else add the sum to the previous indexes map.Print the starting index ... Read More

Largest subarray having sum greater than k in C++

Hafeezul Kareem
Updated on 09-Apr-2021 14:00:21

415 Views

In this tutorial, we are going to write a program that finds the largest subarray have sum greater than k.Let's see the steps to solve the problem.Initialise the array.Iterate over the array and store sum at each index in a vector along with the index.Sort the above sums based on sum and index.Initialise an array to store the indexes.Write a loop that iterates till n.Update the values with min index of above indexes array and previous sums array index.Initialise sum to 0.Write a loop that iterates till n.Add current element to sum.If the sum is greater than k.The maximum subarray ... Read More

Largest smaller number possible using only one swap operation in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:58:06

203 Views

In this tutorial, we are going to write a program that finds the largest number with a single swap that is less than the given number n.Let's see the steps to solve the problem.Initialise the number n.Iterate from the end of the string and find the index of the digit which is greater than its next digit. Store it in a variable.Break the loop as soon as u find it.Iterate over the number from the end of the string to the above index.Find the index of the digit which is less the above indexed digit and is greater among all ... Read More

Largest set with bitwise OR equal to n in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:55:39

122 Views

In this tutorial, we are going to write a program that finds the largest set with bitwise OR is equal to the given number n.Let's see the steps to solve the problem.Initialise the number n.Write a loop that iterates from 0 to n.If the i | n is equal to n, then add i to the result.Return the result.ExampleLet's see the code. Live Demo#include using namespace std; void printBitWiseOrSet(int n) {    vector v;    for (int i = 0; i

Largest permutation after at most k swaps in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:55:19

671 Views

In this tutorial, we are going to write a program that finds the largest permutation after at most k swaps.Let's see the steps to solve the problem.Initialise the array.Initialise an array to store the index with size n + 1.Iterate over the array and store the index of each element in the position array.Write a loop that iterate till n and k is greater than 0.Store the position of n - i element in a temp variable.Update position of current element arr[i] with position[n - i].Update the position position[n - i] with current index i.Swap the current element arr[i] with ... Read More

Advertisements