Found 10476 Articles for Python

How to create a Swarm Plot with Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 12:55:54

1K+ Views

To create a Swarm Plot with Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe, i.e., a two-dimensional, size-mutable, potentially heterogeneous tabular data.Initialize the plotter, swarmplot.To plot the boxplot, use boxplot() method.To display the figure, use show() method.Exampleimport seaborn as sns import matplotlib.pyplot as plt import pandas as pd import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = pd.DataFrame({"Box1": np.arange(10),                      "Box2": np.arange(10)}) ax = sns.swarmplot(x="Box1", y="Box2", data=data, zorder=0) ... Read More

How to display the matrix value and colormap in Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 12:55:08

4K+ Views

To display the matrix value and colormap 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.Initialize max and min values for matrix.Plot the values of a 2D matrix or array as color-coded image.Iterate each cell of the color-code image and place value at the center.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() min_val, max_val = 0, 5 matrix = np.random.randint(0, 5, size=(max_val, ... Read More

How to annotate a range of the X-axis in Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 12:54:32

2K+ Views

To annotate a range of 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 xx and yy data points using numpy.Create a figure and a set of subplots.Plot xx and yy data points using plot() method.Set ylim of the axis.Use annotate method to place arrow heads and range tag name.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 xx = np.linspace(0, 10) yy = np.sin(xx) fig, ax = plt.subplots(1, 1) ... Read More

How to annotate several points with one text in Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 12:48:54

2K+ Views

To add annotated text in Matplotlib for several points, 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.To set the label for each scattered point, make a list of labels.Plot xpoints, ypoints using scatter() method. For color, use xpoints.Iterate zipped labels, xpoints and ypoints.Use annotate() method with bold LaTeX representation in a for loop.To display the figure, use 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 xpoints = np.linspace(1, 10, 10) ... Read More

How to plot a line in Matplotlib with an interval at each data point?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 12:47:05

3K+ Views

To plot a line in Matplotlib with an interval at each data point, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make an array of means and standard deviations.Plot means using plot() method.Fill the area between means+stds and means-stds, alpha=0.7 and color='yellow'.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True means = np.array([3, 5, 1, 8, 4, 6]) stds = np.array([1.3, 2.6, 0.78, 3.01, 2.32, 2.9]) plt.plot(means, color='red', lw=7) plt.fill_between(range(6), means - stds, means ... Read More

How to plot CSV data using Matplotlib and Pandas in Python?

Rishikesh Kumar Rishi
Updated on 26-Aug-2023 03:10:30

44K+ Views

To plot CSV data using Matplotlib and Pandas 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 headers of the .CSV file.Read the CSV file with headers.Set the index and plot the dataframe.To display the figure, use show() method.Exampleimport pandas as pd import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True headers = ['Name', 'Age', 'Marks'] df = pd.read_csv('student.csv', names=headers) df.set_index('Name').plot() plt.show()Output

How to create a line chart using Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 12:46:01

917 Views

To create a line chart using matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make lists of years and population growth.Plot years and population on the line using plot() method.To 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 years = [1901, 1911, 1921, 1931, 1941, 1951,         1961, 1971, 1981, 1991, 2001, 2011] population = [237.4, 238.4, 252.09, 251.31, 278.98, 318.66, 361.09, 439.23, 548.16, 683.33, 846.42, 1028.74] plt.plot(years, population, color='red', marker='o') plt.show()Output

How to use different markers for different points in a Pylab scatter plot(Matplotlib)?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 12:45:12

7K+ Views

To use different markers for different points in a Pylab (Pyplot) scatter plot, we can use the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable, N, for number of sample data.Create x and y random data points.Make a list of markers.Zip the x, y and markers.Iterate the zipper objects and plot the data points with different markers.To display the figure, use 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 N = 10 x = np.random.rand(N) y = np.random.rand(N) ... Read More

How to show an image in Matplotlib in different colors with different channels?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 12:44:25

3K+ Views

To slice an image into Red, Green and Blue channels with misc.imread, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Read an image from a file into an array.Make lists of colormaps and titles.Create a figure and a set of subplots.Zip the axes, images, titles and colormaps.Iterate zipped objs and set the title of each channel image.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True image = plt.imread('bird.png') titles = ['With red channel', 'With green channel', 'With blue channel'] cmaps ... Read More

How to add a second X-axis at the bottom of the first one in Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 13:00:22

1K+ Views

To add a second X-axis at the bottom of the first one in Matplotlib, we can take the followingStepsSet the figure size and adjust the padding between and around the subplots.Get the current axis (ax1) using gca() method.Create a twin axis (ax2) sharing the Y-axis.Set X-axis ticks at AxisSet X-axis labels at Axis 1 andTo display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True ax1 = plt.gca() ax2 = ax1.twiny() ax2.set_xticks([1, 2, 3, 4, 5]) ax1.set_xlabel("X-axis 1") ax2.set_xlabel("X-axis 2") plt.show()Output

Advertisements