Found 10476 Articles for Python

How to make a rug plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 10:37:03

806 Views

Rug plots are used to visualize the distribution of data. It is a plot of data for a single variable, displayed as marks along an axis. To make a rug plot in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x data points using numpy.Add representation of a kernel-density estimate using Gaussian kernels, kde1 and kde2.Create a new figure or activate an existing figure using figure() method.Add an 'ax1' to the figure as part of a subplot arrangement.Make a rug plot with marker_size=20.Plot x_eval, kde1(x_eval) and kde2(x_eval) data ... Read More

How to fill rainbow color under a curve in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:54:32

713 Views

To fill rainbow color under a curve in Python Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a user-defined method, plot_rainbow_under_curve(), that could have a list of 7 rainbow colors and create a set of data points "x" using numpy.Iterate in the range of 0 to 7 and plot the curve and fill the area between that curve.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 def plot_rainbow_under_curve(): rainbow_colors = ['violet', 'indigo', ... Read More

How to draw axis lines inside a plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:38:32

547 Views

To draw axis lines inside a plot 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.Create x data points using numpy.Add an 'ax' to the figure as part of a subplot arrangement.Plot x and x**x data points using plot() method.Set the left and bottom positions at 0, whereas color of the right and top spines none.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 fig = ... Read More

How to set same scale for subplots in Python using Matplotlib?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:32:07

3K+ Views

To set the same scale for subplot in Python using 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.Add an 'ax1' to the figure as part of a subplot arrangement with nrows=2, ncols=1 and index=1.Add another axis 'ax2' to the figure as part of a subplot arrangement with nrows=2, ncols=1 and index=2, with shared X-axis (to set same scale for subplots)Create "t" data points to plot sine and cosine curves on axes ax1 and ax2.To display the figure, use show() method.Exampleimport ... Read More

Conditional removal of labels in Matplotlib pie chart

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:29:16

2K+ Views

To remove labels from a Matplotlib pie chart based on a condition, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe of wwo-dimensional, size-mutable, potentially heterogeneous tabular data.Plot a pie chart, using pie() method with conditional removal of labels, such that if %age value is greater than 25, then only keep labels, otherwise remove them.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create a ... Read More

Matplotlib – Make a Frequency histogram from a list with tuple elements in Python

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:23:49

3K+ Views

To make a frequency histogram from a list with tuple elements in Python, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a list of tuples, data.Make lists of frequency and indices, after iterating the data.Make a bar plot usig bar() 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 data = [("a", 1), ("c", 3), ("d", 4), ("b", 2), ("e", 7), ("f", 3), ('g', 2)] ind = [] fre = [] for item in data: ... Read More

How to rotate a simple matplotlib Axes?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:18:12

12K+ Views

To rotate a simple matplotlib axes, we can take the following steps −Import the required packages −import matplotlib.pyplot as plt from matplotlib.transforms import Affine2D import mpl_toolkits.axisartist.floating_axes as floating_axesSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Make a tuple of axes extremes.Add a mutable 2D affine transformation, "t". Add a rotation (in degrees) to this transform in place.Add a transform from the source (curved) coordinate to target (rectilinear) coordinate.Add a floating axes "h" with the current figure with GridHelperCurveLinear() instance.Add an 'ax' to the figure as part of a ... Read More

How to add a 3d subplot to a matplotlib figure?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:09:37

3K+ Views

To add a 3D subplot to a matplotlib figure, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x, y and z data points using numpy.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement with projection='3d'.Plot x, y and z data points using plot() method.To display the figure, use .show() method.Examplefrom matplotlib import pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create x, y and ... Read More

How to delete a column from Pandas DataFrame

AmitDiwan
Updated on 16-Sep-2021 09:31:48

1K+ Views

To delete a column from a DataFrame, use del(). You can also use pop() method to delete. Just drop it using square brackets. Mention the column to be deleted in the brackets and that’s it, for example −del dataFrame[‘ColumnName’]Import the required library with an alias −import pandas as pdCreate a Pandas DataFrame −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Now, delete a column “Car” from a DataFrame −del ... Read More

Python – Sort Strings by Case difference

AmitDiwan
Updated on 16-Sep-2021 09:26:40

186 Views

When it is required to sort strings based on case difference, a method is defined that takes a string as a parameter. This method uses list comprehension and ‘isupper’ and ‘islower’ methods along with list comprehension to get case difference. Their difference gives the sorted values.ExampleBelow is a demonstration of the samedef get_diff(my_string):    lower_count = len([ele for ele in my_string if ele.islower()])    upper_count = len([ele for ele in my_string if ele.isupper()])    return abs(lower_count - upper_count) my_list = ["Abc", "Python", "best", "hello", "coders"] print("The list is :") print(my_list) my_list.sort(key=get_diff) print("Sorted Strings by case ... Read More

Advertisements