Attach Pyplot Function to Figure Instance in Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:42:22

234 Views

To attach a pyplot function to a figure instance, we can use figure() method and add an axes to it.StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure() method.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Set a title to this axis using set_title() method.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 fig = plt.figure() ax = fig.add_subplot() ax.set_title("My Title!") plt.show()OutputRead More

Plot Events on Time Using Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:41:48

978 Views

To plot events on time using Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplotsMake a list of data points, where event could occur.Plot a horizontal line with y, xmin and xmax.Plot identical parallel lines at the given positions.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 a = [1, 2, 5, 6, 9, 11, 15, 17, 18] plt.hlines(1, 0, 24) plt.eventplot(a, orientation='horizontal', colors='b') plt.show()Output

Add Legend to imshow in Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:38:40

5K+ Views

To add legend to imshow() in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create random data using numpy.Initialize a color map.Get the unique data points from sample data, step 2.Plot each color with different labels and color, to place on the legend.Place a legend at the upper right corner within a box.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, cm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(3, 3) cmap = cm.YlOrBr unique_data = np.unique(data) i ... Read More

Pause Pylab Figure Until Key Pressed or Mouse Clicked in Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:38:03

926 Views

To pause a pylab figure until a key is pressed of mouse is clicked, we can use"button_press_event" key event.StepsSet the figure size and adjust the padding between and around the subplots.Set the "TkAgg" background.Turn the interactive mode ON.Create a new figure or activate an existing figure.Make a variable, pause=False.Whenever "button_press_event", pause the figure.Bind the function to the event.Create data, x and y data points using numpy.Iterate a True loop to change the plot line and color.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = ... Read More

Change Linewidth and Markersize Separately in FactorPlot in Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:36:55

256 Views

To change the linewidth and markersize separately in a factorplot, we can use the following steps −Set the figure size and adjust the padding between and around the subplots.Load an example dataset from the online repository.Use factorplot() method with scale to change the marker size.To display the figure, use show() method.Exampleimport seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True exercise = sns.load_dataset("exercise") g = sns.factorplot(x="time", y="pulse", hue="kind", data=exercise, ci=95,                   markers=['o', '*', 'd'],                 ... Read More

Change Space Between Bars in Multiple Barplots using Pandas and Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:36:13

7K+ Views

To change the space between bars when drawing multiple barplots in Pandas within a group, we can use linewidth in plot() method.StepsSet the figure size and adjust the padding between and around the subplots.Make a dictionary with two columns.Create a two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot the dataframe with plot() method, with linewidth that change the space between the bars.Place a legend on the plot.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True d = {'Column 1': [i for i in range(10)],     ... Read More

Install Matplotlib without QT using Conda on Windows

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:35:43

603 Views

To install Matplotlib package with Conda, run one of the following −conda install -c conda-forge matplotlib-base conda install -c conda-forge/label/testing matplotlib-base conda install -c conda-forge/label/testing/gcc7 matplotlib-base conda install -c conda-forge/label/cf202003 matplotlib-base conda install -c conda-forge/label/matplotlib_rc matplotlib-base conda install -c conda-forge/label/gcc7 matplotlib-base conda install -c conda-forge/label/broken matplotlib-base conda install -c conda-forge/label/matplotlib-base_rc matplotlib-base conda install -c conda-forge/label/rc matplotlib-base conda install -c conda-forge/label/cf201901 matplotlib-base

Reuse Plots in Matplotlib

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:35:15

2K+ Views

To reuse plots in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure() method.Plot a line with some input lists.To reuse the plot, update y data and the linewidth of the plotTo 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 line, = plt.plot([1, 3], [3, 4], label="line plot", color='red', lw=0.5) line.set_ydata([3.5]) line.set_linewidth(4) plt.show()Output

Modify Matplotlib Legend After Creation

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:34:44

2K+ Views

To modify a Matplotlib legend after it has been created, we can have multiple methods to modify the created legend.Set the figure size and adjust the padding between and around the subplots.Plot a line using plot() method, with two lists and a label.Use legend() method to place a legend over the plot.To modify the matplotlib legend, use set_title() 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 plt.plot([1, 3, 4, 5, 2, 1], [3, 4, 1, 3, 0, 1],          label="line plot", color='red', lw=0.5) ... Read More

Plotting Error Bars from DataFrame using Seaborn FacetGrid

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:34:13

597 Views

To plot error bars from a dataframe using Seaborn FacetGrid, we can use following steps −Get a two-dimensional, size-mutable, potentially heterogeneous tabular data.Multi-plot grid for plotting conditional relationships.Apply a plotting function to each facet's subset of the data.To display the figure, use show() method.Exampleimport pandas as pd import seaborn as sns from matplotlib import pyplot as plt df = pd.DataFrame({'col1': [3.0, 7.0, 8.0],                   'col2': [1.0, 4.0, 3.0]}) g = sns.FacetGrid(df, col="col1", hue="col1") g.map(plt.errorbar, "col1", "col2", yerr=0.75, fmt='o') plt.show()Output

Advertisements