
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10476 Articles for Python

9K+ Views
To create a custom mouse cursor in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Bind the function *mouse_event* to the event *button_press_event*.Create x and y data points using numpy.Plot the x and y data points using plot() method.To display the figure, use Show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True def mouse_event(event): print('x: {} and y: {}'.format(event.xdata, event.ydata)) fig = plt.figure() cid = fig.canvas.mpl_connect('button_press_event', mouse_event) ... Read More

1K+ Views
To read an input image and print it into an array in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Read an image from a file into an array. Use plt.imread() method.Print the Numpy array of the image.To turn off the axis, use axis('off') method.To display the figure, use Show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True im = plt.imread("forest.jpg") print("Numpy array of the image is: ", im) im = plt.imshow(im) plt.axis('off') plt.show()OutputIt will produce the following output −On the ... Read More

1K+ Views
Suppose, we have to build a string 'str' that is of length n. To build the string, we can perform two operations.A character can be added to the end of str for cost a.A substring sub_str can be added to the end of the str for cost r.We have to calculate the minimum cost of building the string str.So, if the input is like a = 5, r = 4, str = 'tpoint', then the output will be 29.To build the string 'tpoint', the cost is described below −str = 't'; a new character added, therefore the cost is 5. ... Read More

1K+ Views
To create minor ticks for a polar plot in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create r (radius) and theta data points using numpy.Add a subplot to the current figure.Iterate the points between 0 to 360 with step=10 and plot them to get the ticks.To display the figure, use Show() method.Exampleimport numpy as np import matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # radius and theta for the polar plot r = np.arange(0, 5, 0.1) theta = 2 ... Read More

2K+ Views
To plot an animated image matrix in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make an animation by repeatedly calling a function *update*.Inside the update method, create a 6×6 dimension of matrix and display the data as an image, i.e., on a 2D regular raster.Turn off the axes using set_axis_off().To display the figure, use Show() method.Examplefrom matplotlib.animation import FuncAnimation import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() def ... Read More

625 Views
To put xtick labels in a box, we can take the following stepsStepsCreate a new figure or activate an existing figure.Get the current axis of the figure.Set the left and bottom position of the axes.Set the position of the spines, i.e., bottom and left.To put xtick labels in a box, iterate the ticklabels and use set_bbox() method.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True plt.figure() ax = plt.gca() ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.spines['bottom'].set_position(('data', 0)) ax.spines['left'].set_position(('data', 0)) for label in ax.get_xticklabels(): label.set_fontsize(12) label.set_bbox(dict(facecolor='red', edgecolor='black', alpha=0.7)) ... Read More

351 Views
Suppose we have a binary string s. Now let us consider an operation, where we split the string into two non-empty substrings s1 and s2. The score of this split is the sum of "0"s count in s1 and sum of "1"s count in s2. We have to find the maximum score we can obtain.So, if the input is like s = "011001100111", then the output will be 8, because we can split the string like "01100" + "110111". Then, the score is 3 + 5 = 8.To solve this, we will follow these steps −ones := number of "1"s ... Read More

2K+ Views
To plot a time as an index value in a Pandas dataframe in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with two columns, time and speed.Set the DataFrame index using existing columns.To display the figure, use Show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Pandas dataframe df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10))) # Set the dataframe index df.set_index('time').plot() # ... Read More

149 Views
Suppose we have a given matrix, We have to find a new matrix res, whose dimension is same as the given matrix where each element in res[i, j] = sum of the elements of matrix[r, c] for each r ≤ i, and c ≤ j.So, if the input is like8274then the output will be8101521To solve this, we will follow these steps −if matrix is empty, thenreturn matrixR := row count of matrixC := column count of matrixfor r in range 1 to R - 1, dofor c in range 0 to C - 1, domatrix[r, c] := matrix[r, c] + ... Read More

628 Views
To put a title for a curved line in Python Matplotlib, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points such that the line would be a curve.Plot the x and y data points.Place a title for the curve plot using plt.title() method.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create x and y data points x = np.linspace(-1, 1, 50) y = 2**x + 1 # Plot ... Read More