Found 1034 Articles for Matplotlib

How to add legend to imshow() in Matplotlib?

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

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

How to pause a pylab figure until a key is pressed or mouse is clicked? (Matplotlib)

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

730 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

How to change the linewidth and markersize separately in a factorplot in Matplotlib?

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

190 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

How to change the space between bars when drawing multiple barplots in Pandas? (Matplotlib)

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

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

How to install Matplotlib without installing Qt using Conda on Windows?

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

448 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

How to reuse plots in Matplotlib?

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

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

How to modify a Matplotlib legend after it has been created?

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 a dataframe using Seaborn FacetGrid (Matplotlib)

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

437 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

How can I get pyplot images to show on a console app? (Matplotlib)

Rishikesh Kumar Rishi
Updated on 18-Jun-2021 10:33:06

2K+ Views

To show pyplot images on a console, we can use pyplot.show() method.StepsSet the figure size and adjust the padding between and around the subplots.Create random data of 5☓5 dimension.Use imshow() method, with data. 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.random.rand(5, 5) plt.imshow(data, cmap="copper") plt.show()Output

How to plot a function defined with def in Python? (Matplotlib)

Rishikesh Kumar Rishi
Updated on 24-Aug-2023 16:23:56

57K+ Views

To plot a function defined with def in Python, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a user-defined function using, def, i.e., f(x).Create x data points using numpy.Plot x and f(x) using plot() method.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 def f(x):    return np.sin(x) + x + x * np.sin(x) x = np.linspace(-10, 10, 100) plt.plot(x, f(x), color='red') plt.show()OutputRead More

Advertisements