Found 10476 Articles for Python

How to color a Seaborn boxplot based on DataFrame column name in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:21:06

587 Views

To color a Seaborn boxplot based on dataframe column name, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a Pandas dataframe with two columns, col1 and col2.Make a boxplot with horizontal orientation.Get the boxes artists.Iterate the boxes and set the facecolor of the box.To display the figure, use show() method.Exampleimport seaborn as sns import matplotlib.pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(       [[2, 4],       [7, 2]       ], columns=['col1', 'col2']) ... Read More

How to draw rounded line ends using Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:18:41

2K+ Views

To draw rounded line ends using matplotlib, we can use solid_capstyle='round'.StepsSet the figure size and adjust the padding between and around the subplots.Create random x and y data points using numpy.Create a figure and a set of subplots.Plot x and y data points using plot() method, with solid_capstyle in the method argument.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 x = np.random.randn(5) y = np.random.randn(5) fig, ax = plt. subplots() ln, = ax.plot(x, y, lw=10, solid_capstyle='round', color='red') plt.show()OutputRead More

How to show date and time on the X-axis in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:17:20

7K+ Views

To show date and time on the X-axis in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a list of dates and y values.Get the current axis.Set the major date formatter and locator.Plot x and y values using plot() method.To display the figure, use show() method.Examplefrom datetime import datetime as dt from matplotlib import pyplot as plt, dates as mdates plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True dates = ["01/02/2020", "01/03/2020", "01/04/2020"] x_values = [dt.strptime(d, "%m/%d/%Y").date() for d in dates] y_values = [1, 2, 3] ax ... Read More

Plot 95% confidence interval errorbar Python Pandas dataframes in Matplotlib

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:15:57

1K+ Views

To plot 95% confidence interval errorbar Python Pandas dataframes, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Get a dataframe instance of two-dimensional, size-mutable, potentially heterogeneous tabular data.Make a dataframe with two columns, category and number.Find the mean and std of category and number.Plot y versus x as lines and/or markers with attached errorbars.To display the figure, use show() method.Exampleimport numpy as np import pandas as pd import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame() df['category'] = np.random.choice(np.arange(10), 1000, replace=True) df['number'] = ... Read More

Creating multiple boxplots on the same graph from a dictionary, using Matplotlib

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:12:31

2K+ Views

To create multiple boxplots on the same graph from a dictionary, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a dictionary, dict, with two columns.Create a figure and a set of subplots.Make a box and whisker plotSet the xtick labels using set_xticklabels() methodTo 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 = {'col1': [3, 5, 2, 9, 1], 'col2': [2, 6, 1, 3, 4]} fig, ax = plt.subplots() ax.boxplot(data.values()) ax.set_xticklabels(data.keys()) plt.show()OutputRead More

How to edit the properties of whiskers, fliers, caps, etc. in a Seaborn boxplot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:11:38

709 Views

To edit the properties of whiskers, fliers, caps, etc. in a Seaborn boxplot, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a dataframe using Pandas.Make a boxplot from the DataFrame columns.Get the boxplot's outliers, boxes, medians, and whiskers data.Print all the above data.To display the figure, use show() method.Exampleimport seaborn as sns import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(age=[23, 45, 21, 15, 12])) _, bp = pd.DataFrame.boxplot(df, return_type='both') outliers = [flier.get_ydata() for flier ... Read More

Plotting Pandas DataFrames in Pie Charts using Matplotlib

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:09:40

404 Views

To plot Pandas data frames in Pie charts using Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a dataframe of two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot the dataframe with activities index using pie() methodTo display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'activities': ['sleep', 'exercise', 'work', 'study'],                                     'hours': [8, 1, 9, 6]}) df.set_index('activities').plot.pie(y='hours', legend=False,                                     autopct='%1.1f%%') plt.show()Output

How to force Matplotlib to show the values on X-axis as integers?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:08:54

18K+ Views

To force matplotlib to show the values on X-axis as integers, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create two lists, x and y, of data points.Plot x and y using plot() method.Make a new list for only integers tick on X-axis. Use math.floor() and math.ceil() to remove the decimals and include only integers in the list.Set x and y labels.Set the title of the figure.To display the figure, use show() method.Exampleimport math from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True y ... Read More

How to plot certain rows of a Pandas dataframe using Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:07:44

9K+ Views

To plot certain rows of a Pandas dataframe, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas data frame, df. It should be a two-dimensional, size-mutable, potentially heterogeneous tabular data.Make rows of Pandas plot. Use iloc() function to slice the df and print specific rows.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(np.random.randn(10, 5), columns=list('abcde')) df.iloc[0:6].plot(y='e') print(df.iloc[0:6]) # plt.show()OutputWe have 10 rows in ... Read More

Moving X-axis in Matplotlib during real-time plot

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:05:03

1K+ Views

To move X-axis in Matplotlib during real-time plot, 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.Create x and y data points using numpy.Plot x and y data points using plot() method.Make an animation by repeatedly calling a function *animate* that moves the X-axis during real-time plot.To display the figure, use show() method.Exampleimport matplotlib.pylab as plt import matplotlib.animation as animation import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.linspace(0, 15, 100) ... Read More

Advertisements