Found 10476 Articles for Python

How to plot an image with non-linear Y-axis with Matplotlib using imshow?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:35:11

966 Views

To plot an image with non-linear Y-axis with matplotlib using imshow() method, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Add a subplot to the current figure.Set nonlinear Y-axis ticks.Create random data points using numpy.Display data as an image, i.e., on a 2D regular raster, with data.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True ax = plt.subplot(111) ax.yaxis.set_ticks([0, 2, 4, 8]) data = np.random.randn(5, 5) plt.imshow(data, cmap='copper') plt.show()OutputRead More

How to create a matplotlib colormap that treats one value specially?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:33:09

597 Views

To create a matplotlib colormap that treats one value specially, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Get a colormap instance, name is "rainbow".Set the color for low out-of-range values, using set_under('red') method.Create random data and eps using numpy.Create a figure and a set of subplots.Display data as an image, i.e., on a 2D regular raster, using imshow() method.Create a colorbar for a ScalarMappable instance, im.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True cmap ... Read More

How to show a figure that has been closed in Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:31:25

964 Views

To show a figure that has been closed in Matplotlib, we can create a new Canvas Manager and store the previous figure into a new Canvas figure.StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Create x and y data points using numpy.Plot x and y data points using plot() method.Close the current figure where the plot has been plotted.Now, store the previous figure in a new Canvas figure.Set the Canvas that contains the figure.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as ... Read More

How to set the Y-axis in radians in a Python plot?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:29:24

935 Views

To set the Y-axis in radians in a Python plot, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data point using numpy.Create a new figure or activate an existing figure using figure() method.Add an axes, ax, to the figure as part of a subplot arrangement.Get the list of Y-axis ticks and ticklabels.Set the ticks and ticklabels using set_yticks() and set_yticklabels() methods.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(-10.0, ... Read More

How to plot histograms from dataframes in Pandas using Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:24:36

586 Views

To plot histograms against Pandas/Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a potentially hetrogeneous tabular data using Pandas dataframe.Use the dataframe to make a histogram.To display the figure, use show() method.Examplefrom 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({'a': [1, 1, 1, 1, 3],    'b': [1, 1, 2, 1, 3],    'c': [2, 2, 2, 1, 3],    'd': [2, 1, 2, 1, 3], }) df.hist() plt.show()Output

How can box plot be overlaid on top of swarm plot in Seaborn?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:22:32

520 Views

To plot a Box plot overlaid on top of a Swarm plot in Seaborn, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data.Initialize the plotter, swarmplot.To plot the box plot, use boxplot() method.To display the figure, use show() method.Exampleimport seaborn as sns import matplotlib.pyplot as plt import pandas as pd import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = pd.DataFrame({"Box1": np.arange(10), "Box2": np.arange(10)}) ax = sns.swarmplot(x="Box1", y="Box2", data=data, zorder=0) sns.boxplot(x="Box1", y="Box2", data=data, showcaps=False, ... Read More

Setting the display range suplot of errorbars in matplotlib

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:17:31

125 Views

To set the display range subplot or errorbars in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots.Plot y versus x as lines and/or markers with attached errorbars.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(0.1, 4, 0.5) y = np.exp(-x) fig, ax = plt.subplots() ax.errorbar(x, y, xerr=0.2, yerr=0.4) plt.show()OutputRead More

Adjust the width of box in boxplot in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:14:33

7K+ Views

To adjust the width of box in boxplot in Python matplotlib, we can use width in the boxplot() method.StepsSet the figure size and adjust the padding between and around the subplots.Make a Pandas dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data.Make a box and whisker plot, using boxplot() method with width tuple to adjust the box in boxplot.To display the figure, use show() method.Exampleimport pandas as pd import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = pd.DataFrame({"Box1": np.random.rand(10), "Box2": np.random.rand(10)}) ax = plt.boxplot(data, widths=(0.25, 0.5)) plt.show()OutputRead More

How to draw a heart with pylab?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:12:27

1K+ Views

To draw a heart with pylab/pyplot, we can follow the steps given below −StepsSet the figure size and adjust the padding between and around the subplots.Create x, y1 and y2 data points using numpy.Fill the area between (x, y1) and (x, y2) using fill_between() method.Place text on the plot using text() method at (0, -1.0) point.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 x = np.linspace(-2, 2, 1000) y1 = np.sqrt(1 - (abs(x) - 1) ** 2) y2 = -3 * np.sqrt(1 - ... Read More

How to save an image with matplotlib.pyplot?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 12:10:48

5K+ Views

To save an image with matplotlib.pyplot.savefig(), we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points using plot() method.To save the figure, use savefig() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-np.pi, np.pi, 100) plt.plot(x, np.sin(x) * x, c='red') plt.savefig("myimage.png")OutputWhen we execute the code, it will save the following image as "myimage.png" in the Project directory.Read More

Advertisements