Found 1034 Articles for Matplotlib

Plot yscale class linear, log, logit and symlog by name in Matplotlib?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:17:35

433 Views

To plot yscale with class by name, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create y data points using numpy.Create x data points using numpy.Add a subplot to the current figure at index 1.Plot x and y data points using plot() method.For linear class by name, use yscale("linear") method.Set the title of the current subplot.Repeat the steps from 4 to 5 with different indices, yscale() class by name, and title of the plot.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] ... Read More

How to locate the median in a (Seaborn) KDE plot?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:14:40

1K+ Views

To locate the median in a seaborn KDE plot, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create random data using numpy.Find the median of data (Step 2).Use kdeplot() to plot the shaded region.Use axvline() method to plot the vertical line.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.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randn(30) xmedian = np.median(data) k = sns.kdeplot(x=data, shade=True) plt.axvline(xmedian, c='red') plt.show()OutputRead More

How to set the margins of a Matplotlib figure?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:06:23

5K+ Views

To set the margins of a matplotlib figure, we can use margins() method.StepsSet the figure size and adjust the padding between and around the subplots.Create t and y data points using numpy.Add a subplot to the current figure at index 1.Plot t and y data points using plot() method.Set the title of the plot.Add a subplot to the current figure at index 2.Plot t and y data points using plot() method.Set the title of the plot.Set margins of the plot using margins(x=0, y=0).To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] ... Read More

Automatically setting Y-axis limits for a bar graph using Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:00:21

8K+ Views

To set Y-axis limit, we can use ylim() method and put maximum and minimum limit values.StepsSet the figure size and adjust the padding between and around the subplots.Create two lists for data points.Make two variables for max and min values for Y-axis.Use ylim() method to limit the Y-axis range.Use bar() method to plot the bars.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 x = [1, 2, 3, 4, 5] y = [8, 4, 6, 1, 3] max_y_lim = max(y) + .5 min_y_lim = min(y) plt.ylim(min_y_lim, max_y_lim) plt.bar(x, y) ... Read More

How do you improve Matplotlib image quality?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:16:42

6K+ Views

To improve matplotlib image quality we can use greater dot per inch i.e dpi value (greater than 600) and pdf or .eps format can be recommended.StepsSet the figure size and adjust the padding between and around the subplots.Make a 2D data raster using a np.array.Display data as an image, i.e., on a 2D regular raster.Save the current image using savefig() with dpi=1200 and .eps format, 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 data = np.array( [[0.1, 0.7, 0.6, 0.3], ... Read More

How to close a Python figure by keyboard input using Matplotlib?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:14:27

1K+ Views

To close a Python figure by a keyboard input, we can use plt.pause() method, an input, and close() method.StepsSet the figure size and adjust the padding between and around the subplots.Create random t and y data points using numpy.Create a new figure or activate an existing figure using figure() method.Plot t and y data points using plot() method.Set the title of the plot.Redraw the current figure using draw() method.Run a true loop to pause the current figure.Take input from the user to go to the next statement.Use close() method to close the figure.Exampleimport numpy as np from matplotlib import pyplot ... Read More

How can I display an np.array with pylab.imshow() using Matplotlib?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:14:10

918 Views

To display an np.array with imshow(), we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Make a 2D data raster using an np.array.Display the data as an image, i.e., on a 2D regular raster.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 data = np.array( [[0.1, 0.7, 0.6, 0.3], [0.2, 0.6, 0.5, 0.2], [0.8, 0.3, 0.80, 0.01], [0.3, 0.4, 0.2, 0.1]] ) plt.imshow(data, interpolation="nearest", cmap="RdYlGn_r") plt.show()Output

How to plot a stacked event duration using Python Pandas?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:15:04

374 Views

To plot a stacked event duration using Python Pandas, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create a dataframe with lists of xmin and its corresponding xmax.Use hlines() method to plot a stacked event duration.To display the figure, use show() method.Exampleimport pandas as pd from datetime import datetime as dt from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(xmin=[dt.strptime('1994-07-19', '%Y-%m-%d'), dt.strptime('2006-03-16', '%Y-%m-%d'), dt.strptime('1980-10-31', '%Y-%m-%d'), dt.strptime('1981-06-11', '%Y-%m-%d'), dt.strptime('2006-06-28', '%Y-%m-%d')], ... Read More

What is the simplest way to make Matplotlib in OSX work in a virtual environment?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:15:36

73 Views

To make matplotlib in OSX work in a virtual environment, we can first create a virtual environment and then activate that created environment. Thereafter, install all the dependencies in that virtual environment.StepsOpen ubuntu terminal.apt-get install python-venvpython -m venv source /bin/activate

Annotate Subplots in a Figure with A, B, C using Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:16:14

1K+ Views

To annotate subplots in a figure with A, B and C using matplotlib, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots, with nrows=1 and ncols=3.Make a 1D iterator over an array.Iterate each axes and display data as an image.In the loop itself, place text A, B and C.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt import string plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, axs = plt.subplots(1, 3) axs = axs.flat for index, ax ... Read More

Advertisements