Programming Articles - Page 1202 of 3363

How to show an Axes Subplot in Python?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:03:30

2K+ Views

To show an axes subplot in Python, we can use show() method. When multiple figures are created, then those images are displayed using show() method.StepsCreate x and y data points using numpy.Plot x and y using plot() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(10) y = np.exp(x) plt.plot(x, y) plt.show()Output

Plot a circle with an edgecolor and hatch in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:01:13

543 Views

To place edge color and hatch of a circle in matplotlib, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add a subplot method to the current axis.Create a circle instance using Circle() class with an edgecolor, hatch and linewidth of the edge.Add a circle path on the plot.To place the text in the circle, use text() method.Scale X and Y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) circle = matplotlib.patches.Circle((0, 0), radius=1, edgecolor="orange", ... Read More

How to fill color above the curve in Matplotlib Program?

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

3K+ Views

To fill color above the curve, we can take the following steps −StepsInitialize the variable n. Initialize x and y data points using numpy.Create a figure and a set of subplots, fig and ax.Plot the curve using plot() method.Using fill_between() method, fill the area between two curves, with 1 value.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 n = 256 X = np.linspace(-np.pi, np.pi, n, endpoint=True) Y = np.sin(2 * X) fig, ax = plt.subplots() ax.plot(X, Y, color='blue', alpha=1.00) ax.fill_between(X, Y, 1, color='blue', alpha=.1) plt.show()OutputRead More

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

2K+ 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

1K+ 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

53K+ 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

4K+ 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

Advertisements