Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

1,016 Articles Published

Articles by Rishikesh Kumar Rishi

Page 11 of 102

Graph k-NN decision boundaries in Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 08-Oct-2021 6K+ Views

To make graph k-NN decision boundaries in matplotlib, we can take the following Steps.StepsSet the figure size and adjust the padding between and around the subplots.Initialize a variable n_neighbors for number of neighbors.Load and return the iris dataset (classification).Create x and y data points.Make lists of dark and light colors.Classifier implementing the k-nearest neighbors vote.Create xmin, xmax, ymin and ymax  data points.Create a new figure or activate an existing figure.Create a contourf plot.Create a scatter plot with X dataset.Set x and y axes labels, titles and scale of the axes.To display the figure, use Show() method.Exampleimport numpy as np import ...

Read More

How to make a mosaic plot in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 08-Oct-2021 2K+ Views

To make a mosaic plot in matplotlib, we can take the following steps.StepsSet the figure size and adjust the padding between and around the subplots.Install statsmodel package (pip install statsmodels). It is required to create mosaic plots. statsmodels is a Python package that provides a complement to scipy for statistical computations including descriptive statistics and estimation and inference for statistical models.Make a dictionary for mosaic plot.Create a mosaic plot from a contingency table.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt from statsmodels.graphics.mosaicplot import mosaic plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Dictionary for mosaic plot ...

Read More

How to plot two violin plot series on the same graph using Seaborn?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 08-Oct-2021 1K+ Views

To plot two violin plot series on the same graph using Seaborn, we can take the following Steps.StepsSet the figure size and adjust the padding between and around the subplots.Load an example dataset from the online repository (requires Internet).Create a violin plot using violinplot() method.To display the figure, use Show() method.Example# Import Seaborn and Matplotlib import seaborn as sns from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Load an example dataset tips = sns.load_dataset("tips") # Create a violin plot using Seaborn sns.violinplot(x="day", y="total_bill", hue="time", data=tips) ...

Read More

How to autosize text in matplotlib Python?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 08-Oct-2021 537 Views

To autosize text in matplotlib, we can make a tight layout and rotate the ticks.StepsSet the figure size and adjust the padding between and around the subplots.Plot data points of the range of 10.Make a list of labels.Put ticks and labels on the X-axis with 30 rotation.To display the figure, use Show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True plt.plot(range(10)) labels = [7 * repr(i) for i in range(10)] plt.xticks(range(10), labels, rotation=30) plt.show() OutputIt will produce the following output −

Read More

How to change the scale of imshow in matplotlib without stretching the image?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 08-Oct-2021 18K+ Views

To change the scale of imshow in matplotlib without stretching the image, we can take the following steps.StepsSet the figure size and adjust the padding between and around the subplots.Create random data points with 4×4 dimension.Display the data as an image, i.e., on a 2D regular raster.Use the extent parameter of imshow to map the image buffer pixel coordinates to a data space coordinate system.Next, set the aspect ratio of the image manually by supplying a value such as "aspect=4" or let it auto-scale by using aspect='auto'. This will prevent stretching of the image. By default,  imshow sets the aspect of ...

Read More

How to use ax.get_ylim() in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 08-Oct-2021 658 Views

To use ax.get_ylim() method in matplotlib, we can take the following steps.StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Create random data points using numpy.Plot y data points using plot() method.Use ax.get_ylim() method to print it.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 fig = plt.figure() # Add an axes to the figure ax = fig.add_subplot(1, 1, 1) ...

Read More

How to plot a density map in Python Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 29-Sep-2021 4K+ Views

To plot a density map in Python, we can take the following steps −Create side, x, y, and z using numpy. Numpy linspace helps to create data between two points based on a third number.Return coordinate matrices from coordinate vectors using side data.Create exponential data using x and y (Step 2).Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, cm, colors import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True side = np.linspace(-2, 2, 15) X, Y = np.meshgrid(side, side) Z = np.exp(-((X - 1) ...

Read More

How to put the title at the bottom of a figure in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 23-Sep-2021 7K+ Views

To put the line title at the bottom of a figure in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable, N, to get the number of sample data.Plot the x and y data points using scatter() method.Set the title at the bottom of the figure in matplotlib, with y=-0.01.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 = 100 x = np.random.rand(N) y = np.random.rand(N) plt.scatter(x, y, c=x, ...

Read More

How to make a simple lollipop plot in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 23-Sep-2021 552 Views

To make a simple lollipop plot in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.Make an ordered dataframe, using sort_values().Make a list in the range of dataframe index.Create a stem plot, using the ordered dataframe.Set xticks and labels using xticks() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'group': list(map(chr, range(65, 85))), 'values': np.random.uniform(size=20)}) ...

Read More

What is the correct way to replace matplotlib tick labels with computed values?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 23-Sep-2021 379 Views

We can use ax.loglog(x, y) and set_major_formatter() methods to replace tick labels with computed values.StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make a plot with log scaling on both the X and Y axis.Set the formatter of the major ticker.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, ticker plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() ax.loglog(np.logspace(0, 5), np.logspace(0, 5)**2) ax.xaxis.set_major_formatter(ticker.LogFormatterExponent()) plt.show()Output

Read More
Showing 101–110 of 1,016 articles
« Prev 1 9 10 11 12 13 102 Next »
Advertisements