Found 10476 Articles for Python

How to convert Matplotlib figure to PIL Image object?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 11:55:36

11K+ Views

To convert matplotlib figure to PIL image object, 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.Plot a list using plot() method.Initialize the in-memory buffer.Save the buffered image.Use PIL image to get the image object.Show the current image.Close the in-memory I/O buffer.Exampleimport io from PIL import Image import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.figure() plt.plot([1, 2]) img_buf = io.BytesIO() plt.savefig(img_buf, format='png') im = Image.open(img_buf) im.show(title="My Image") img_buf.close()OutputRead More

How to draw node colormap in NetworkX/Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 11:54:54

609 Views

To draw node colormap in matplotlib/netwokx, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Return the cycle graph $C_n$ of cyclically connected nodes.Position the nodes on a circle.Draw the graph G with Matplotlib.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import networkx as nx plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True G = nx.cycle_graph(24) pos = nx.circular_layout(G) nx.draw(G, pos, node_color=range(24), node_size=800, cmap='copper') plt.show()Output

Updating the X-axis values using Matplotlib animation

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 11:40:01

2K+ Views

To update the X-axis values using Matplotlib animation, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Create x and y data points using numpy.Plot x and y data points using plot method on axis (ax).Make an animation by repeatedly calling a function animate that sets the X-axis value as per the frame.To display the figure, use show() method.Exampleimport matplotlib.pylab as plt import matplotlib.animation as animation import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x ... Read More

Set two Matplotlib imshow plots to have the same colormap scale

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 11:40:43

3K+ Views

To set two matplotlib imshow() plots to have the same colormap scale, we can take the followingStepsSet the figure size and adjust the padding between and around the subplots.Create d1 and d2 matrices using Numpy.Get the resultant matrix to get the maximum and minmum value.Use amin and amax methods for minimum and maximum values.Create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement, with nrows=1, ncols=2 at index 1Using imshow() method with vmin and vmax, define the data range that the colormap covers.Repeat steps 6 and 7 with dataTo display ... Read More

How to apply a mask on the matrix in Matplotlib imshow?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 13:26:27

2K+ Views

To apply a mask on the matrix in matplotlib imshow(), we can use np.ma.masked_where() method with lower and upper limit.StepsInitialize two variables, l and u, to mask the input matrix.Create random data of 5×5 dimension.Mask the input matrix, lower of l value, and above of u.Create a figure and a set of subplots with nrows=1 and ncols=Display the data as an image, i.e., on a 2D regular raster, at axes 0 andSet the title of the axes, 0 andTo 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 ... Read More

How to show the Logarithmic plot of a cumulative distribution function in Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 13:22:52

527 Views

To show the Logarithmic plot of a cumulative distribution function 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, for number of sample data.Create data, X2 and F2 using numpy.Plot X2 and F2 using plot() method.Make x and y scale logarithmic.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 N = 100 data = np.random.randn(N) X2 = np.sort(data) F2 = np.array(range(N))/float(N) plt.plot(X2, F2) plt.xscale('log') plt.yscale('log') plt.show()OutputRead More

How to visualize scalar 2D data with Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 13:21:09

478 Views

To visualize scalar 2D data with matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable, N, for data samples.Create x and y data points using numpy.Get coordinate matrices from coordinate vectors.Get z data points using numpy.Create a pseudocolor plot with a non-regular rectangular grid.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 = 256 x = np.linspace(-3., 3., n) y = np.linspace(-3., 3., n) X, Y = np.meshgrid(x, ... Read More

How to use pyplot.arrow or patches.Arrow in matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 13:19:52

786 Views

To use pyplot.arrow or patches.Arrow() in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize four variables, x_tail, y_tail, x_head and y_head.Create a figure and a set of subplots.Get a fancy arrow instance.Add an artist (step 4) using add_patch() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, patches as mpatches plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x_tail = 0.1 y_tail = 0.1 x_head = 0.9 y_head = 0.9 fig, ax = plt.subplots() arrow = mpatches.FancyArrowPatch((x_tail, y_tail), (x_head, y_head), mutation_scale=100, color='green') ... Read More

How to add black border to matplotlib 2.0 'ax' object In Python 3?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 13:17:02

5K+ Views

To add black border to matplotlib 2.0 'ax' object in Python, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Set axes edgecolor to black.Set axes linewidth to 2.50.Initialize a variable, N, to get the number of sample data.Create x and y data points using numpy.Plot x and y data points using plot() method.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 plt.rcParams["axes.edgecolor"] = "black" plt.rcParams["axes.linewidth"] = 2.50 N = 10 x = np.random.randint(low=0, high=N, size=N) y ... Read More

How to adjust 'tick frequency' in Matplotlib for string Y-axis?

Rishikesh Kumar Rishi
Updated on 03-Aug-2021 13:15:28

747 Views

To adjust tick frequency for for Y-axis, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable, N, for number of sample data points.Create x and y data points using numpy.Plot x and y data points using plot() method.Initialize a variable freq_y to adjust the frequency of the yticks.Use yticks() method to set the yticks.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 N = 10 x = np.random.randint(low=0, high=N, size=N) y = np.random.randint(low=0, high=N, ... Read More

Advertisements