Found 1034 Articles for Matplotlib

How to create a stacked bar chart for my DataFrame using Seaborn in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:00:15

1K+ Views

To create a stacked bar chart, we can use Seaborn's barplot() method, i.e., show point estimates and confidence intervals with bars.Create df using Pandas Data Frame.Using barplot() method, create bar_plot1 and bar_plot2 with color as red and green, and label as count and select.To enable legend, use legend() method, at the upper-right location.To display the figuree, use show() method.Exampleimport pandas import matplotlib.pylab as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pandas.DataFrame(dict(    number=[2, 5, 1, 6, 3],    count=[56, 21, 34, 36, 12],    select=[29, 13, 17, 21, 8] )) bar_plot1 = sns.barplot(x='number', y='count', data=df, label="count", color="red") bar_plot2 = ... Read More

Plot a histogram with colors taken from colormap in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:59:54

1K+ Views

To plot a histogram with colors taken from colormap, we can use the setp() method.StepsCreate data points using numpy.Plot data (Step 1) using hist() method, with bins=25, rwidth=.75, ...etc.Returned values n, bins and patches can help to find col.Get a colormap instance for name "RdYlBu".Zip the col and patches.Now, using setp() method, set the property of each patch.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 data = np.random.random(1000) n, bins, patches = plt.hist(data, bins=25, density=True, color='red', rwidth=0.75) col = (n-n.min())/(n.max()-n.min()) cm = plt.cm.get_cmap('RdYlBu') for c, p in zip(col, ... Read More

Matplotlib figure to image as a numpy array

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:59:03

3K+ Views

We can use the following steps to convert a figure into a numpy array −Read a figure from a directory; convert it into numpy array.Use imshow() method to display the image.Use show() method to display it.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True im = plt.imread("bird.jpg") print("Numpy array of the image is: ", im) im = plt.imshow(im) plt.show()OutputWhen we execute the code, it will show "bird.jpg" in a plot and show its numpy array on the console.Numpy array of the image is: [[[162 162 170] [162 162 170] [160 163 170] ... [ 97 98 92] [ 98 100 95] [ 94 96 91]] [[159 159 167] [159 159 167] [157 160 167] ... [ 94 95 89] [ 95 97 92] [ 92 94 89]] [[157 158 163] [157 158 163] [154 157 164] ... [ 93 94 89] [ 95 95 93] [ 95 95 93]] ... [[163 163 165] [163 163 165] [164 164 164] ... [187 165 151] [158 131 112] [133 105 84]] [[163 163 165] [163 163 165] [163 163 163] ... [160 134 117] [143 112 92] [127 96 75]] [[164 164 166] [163 163 165] [163 163 163] ... [145 116 98] [129 98 78] [124 92 71]]]

Auto adjust font size in Seaborn heatmap using Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:58:28

2K+ Views

To adjust font size in Seaborn, we can take followig steps−Create a dictionary with some mathematical expressionsCreate a dataframe using Pandas data frame.Create a heatmap using heatmap() method.To adjust the font size in Seaborn heatmap, change the fontsize value.To display the figure, use show() method.Exampleimport numpy as np import seaborn as sns from matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True d = {    'y=1/x': [1 / i for i in range(1, 10)],    'y=x': [i for i in range(1, 10)],    'y=x^2': [i * i for i in range(1, 10)], ... Read More

Drawing multiple figures in parallel in Python with Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:57:56

801 Views

To draw multiple figures in parallel in Python with matplolib, we can take the following steps−Create random data using numpy.Add a subplot to the current figure, nrows=1, ncols=4 and at index=1.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="Blues_r".Add a subplot to the current figure, nrows=1, ncols=4 and at index=2.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="Accent_r".Add a subplot to the current figure, nrows=1, ncols=4 and at index=3.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="terrain_r"Add a subplot ... Read More

Plot data from CSV file with Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Sep-2023 23:24:14

41K+ Views

To extract CSV file for specific columns to list in python, we can use Pandas read_csv() method.StepsMake a list of columns that have to be extracted.Use read_csv() method to extract the CSV file data into a data frame.Print the exracted data.Plot the data frame using plot() method.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 = ["Name", "Marks"] df = pd.read_csv("input.csv", usecols=columns) print("Contents in csv file:", df) plt.plot(df.Name, df.Marks) plt.show()OutputRead More

How do I show logarithmically spaced grid lines at all ticks on a log-log plot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:57:03

2K+ Views

To show logarithmically spaced grid lines at all ticks on a log-log plot using matplotlib, we can take the following steps−Create data points for x and y using numpy.Using loglog method, make a plot with log scaling on both the X and Y axis.Use grid() method, lay out a grid in the current line style. Supply the list of x an y positions.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 x = np.arange(0, 10, 1) y = np.exp(x) plt.loglog(x, y, c='r') plt.grid(True, which="both", axis='x') plt.show()OutputRead More

Set Max value for color bar on Seaborn heatmap using Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:52:19

499 Views

To set a value for color bar on Seaborn heatmap, we can take following Steps−Create random data using numpy.Use heatmap() method to plot rectangular data as a color-encoded matrix.To display the figure, use show() method.Exampleimport numpy as np import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(4, 4) ax = sns.heatmap(data, vmax=1) plt.show()Output

Putting arrowheads on vectors in Matplotlib's 3D plot

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:47:53

2K+ Views

To draw arrow heads vectors in 3D matplotlb's plot, we can take the following steps −Create a 2D array, where x, y, z, u, v and w are the coordinates of the arrow locations and direction components of arrow vectors.Using figure() method, create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement, using add_subplot() method.Plot a 3D field of arrows, using quiver() method.Using ylim, xlim, zlim, limit the range of the axes.Set the title of the plot.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"] = ... Read More

Extract csv file specific columns to list in Python

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:45:13

15K+ Views

To extract csv file for specific columns to list in Python, we can use Pandas read_csv() method.StepsMake a list of columns that have to be extracted.Use read_csv() method to extract the csv file into data frame.Print the exracted data.Plot the data frame using plot() method.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 = ["Name", "Marks"] df = pd.read_csv("input.csv", usecols=columns) print("Contents in csv file:", df) plt.plot(df.Name, df.Marks) plt.show()The csv file contains the following data −NameMarksArun98Shyam75Govind54Javed92Raju87OutputWhen we execute the code, it will extract the data from the csv file ... Read More

Advertisements