Found 10476 Articles for Python

Python – Find the sum of Length of Strings at given indices

AmitDiwan
Updated on 20-Sep-2021 06:43:55

286 Views

When it is required to find the sum of the length of string at specific indices, the ‘enumerate’ is used to iterate through the elements in the list and adding the length of the element to a list.ExampleBelow is a demonstration of the samemy_list = ["python", "is", "best", "for", "coders"] print("The list is :") print(my_list) index_list = [0, 1, 4] result = 0 for index, element in enumerate(my_list): if index in index_list: result += len(element) print("The result is :") print(result)OutputThe list is : ['python', 'is', 'best', 'for', ... Read More

Python – Test if elements of list are in Min/Max range from other list

AmitDiwan
Updated on 20-Sep-2021 06:42:12

568 Views

When it is required to test if the elements are in the min/max range, the list elements are iterated over, and are checked to see if it is equal to ‘max’ value.ExampleBelow is a demonstration of the samemy_list = [5, 6, 4, 7, 8, 13, 15] print("The list is : ") print(my_list) range_list = [4, 7, 10, 6] my_result = True for elem in range_list: if elem!= max(my_list): my_result = False break if(elem == True): print("All the elements are ... Read More

Python – Mean deviation of Elements

AmitDiwan
Updated on 20-Sep-2021 06:37:24

795 Views

When it is required to find the mean deviation of the elements of a list, the ‘sum’ method and the ‘len’ method is used.ExampleBelow is a demonstration of the samemy_list = [3, 5, 7, 10, 12] print("The list is :") print(my_list) my_mean = sum(my_list) / len(my_list) my_variance = sum([((x – my_mean) ** 2) for x in my_list]) / len(my_list) my_result = my_variance ** 0.5 print("The result is :") print(result)OutputThe original list : [3, 5, 7, 10, 12] the standard deviation of list is : 3.2619012860600183ExplanationA list is defined and is displayed on the console.The ‘sum’ of the ... Read More

How to make a scatter plot for clustering in Python?

Rishikesh Kumar Rishi
Updated on 19-Sep-2021 08:00:50

7K+ Views

To make a scatter plot for clustering in Python, 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, Cluster and centers using numpy.Create a new figure or activate an existing figure.Add a subplot arrangement to the current figure.Plot the scatter data points using scatter() method.Iterate centers data and place marker using scatter() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.randn(10) y = np.random.randn(10) Cluster = np.array([0, 1, ... Read More

How do I put a circle with annotation in matplotlib?

Rishikesh Kumar Rishi
Updated on 19-Sep-2021 07:28:03

3K+ Views

To put a circle with annotation in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create data points using numpy.Get the point coordinate to put circle with annotation.Get the current axis.Plot the data and data points using plot() method.Set X and Y axes scale.To put a circled marker, use the plot() method with marker='o' and some properties.Annotate that circle (Step 7) with arrow style.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 data = np.array([[5, ... Read More

Creating 3D animation using matplotlib

Rishikesh Kumar Rishi
Updated on 22-Sep-2021 08:59:38

6K+ Views

To create a 3D animation using matplotlib, we can take the following steps −Import the required packages. For 3D animation, you need to import Axes3D from mpl_toolkits.mplot3d and matplotlib.animation.Set the figure size and adjust the padding between and around the subplots.Create t, x, y and data points using numpy.Create a new figure or activate an existing figure.Get the instance of 3D axes.Turn off the axes.Plot the lines with data.Create an animation by repeatedly calling a function *animate*.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from mpl_toolkits.mplot3d import Axes3D plt.rcParams["figure.figsize"] ... Read More

How to set labels in matplotlib.hlines?

Rishikesh Kumar Rishi
Updated on 19-Sep-2021 08:10:07

4K+ Views

To set labels in matplotlib.hlines, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Add a horizontal line across the axis, y=1, with y=1 label, color='orange'.Add a horizontal line across the axis, y=2, with y=2 label, color='red'.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Add horizontal line plt.hlines(y=1, xmin=1, xmax=4, lw=7, color='orange') plt.text(4, 1, 'y=1', ha='left', va='center') # Add another horizontal line plt.hlines(y=2, xmin=2, xmax=5, lw=7, color='red') plt.text(2, 2, 'y=2', ha='right', va='center') ... Read More

How to create broken horizontal bar graphs in matplotlib?

Rishikesh Kumar Rishi
Updated on 22-Sep-2021 08:52:51

296 Views

To create broken horizontal bar graphs 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.Plot a horizontal sequence of rectangles.Scale X and Y axes limit.Configure the grid lines.Annotate the broken bars.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 fig, ax = plt.subplots() # Horizontal sequence of rectangles ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue') ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9),    facecolors=('tab:orange', 'tab:green', 'tab:red')) # Scale ... Read More

How to modify a 2d Scatterplot to display color based on a third array in a CSV file?

Rishikesh Kumar Rishi
Updated on 22-Sep-2021 08:47:24

327 Views

To modify a 2d scatterplot to display color based on a third array in a CSV file, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Read the CSV file with three headers.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Make a scatter plot with CSV file data points.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True columns = ["data1", "data2", "data3"] df = ... Read More

How to turn off the ticks and marks of a matlibplot axes?

Rishikesh Kumar Rishi
Updated on 10-Sep-2023 08:25:31

51K+ Views

To turn off the ticks and marks of a matplotlib axes, 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() method.Get the current axis of the plot.Use set_tick_params() to hide X and Y axes label marks.Use set_xticks() and set_yticks() to hide X and Y axes tick marks.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ... Read More

Advertisements