Matplotlib Articles

Page 11 of 91

How to cycle through both colours and linestyles on a matplotlib figure?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 19-Oct-2021 2K+ Views

To cycle through both colors and linestyles on a matplotlib figure, we can take the following steps.StepsSet the figure size and adjust the padding between and around the subplots.Set the current rcParams, withcolors and linestyle.Plot the data points using plot() method.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt from cycler import cycler # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Set the rcParams with color or linestyle plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) + cycler('linestyle', [':', '-.', '-', '--']))) # Plot the data points plt.plot([0, 5, 2, ...

Read More

How to better rasterize a plot without blurring the labels in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 19-Oct-2021 671 Views

To rasterize a plot in a bettery way without blurring the labels in matplotlib, we can take the following steps.StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Axis 0 – Fill the area between the curve with alpha and rasterized=False.Add text to the axes.Axis 1 – Fill the area between the curve with alpha and rasterized=True.Add text to the axes.Axes 2 and 3 – Fill the area between the curve without alpha and rasterized=True and False, respectively.Add text to the axes.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as ...

Read More

How to get the properties of a picked object in mplot3d (matplotlib + python)?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 19-Oct-2021 384 Views

To get the properties of picked objects in matplotlib 3d, we can take the following steps.StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Make a scatter plot of random data points.Bind the function *pick_event_method* to the event *pick_event*.Print x, y and z coordinates of the event.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ...

Read More

Plotting profile histograms in Python Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 19-Oct-2021 793 Views

In a profile histogram, each bin contains the mean of its entries. To plot profile histograms in Python, we can use the regplot method from Seaborn.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Use seaborn.regplot to plot the data and a linear regress model fit. Use the parameter x_bins to bin the x variable into discrete bins. Use fit_reg=True to plot the regression model relating the x and y variables.To display the figure, use Show() method.Exampleimport numpy as np import seaborn as sns from matplotlib import pyplot as plt ...

Read More

Matplotlib – Difference between plt.subplots() and plt.figure()

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Oct-2021 2K+ Views

plt.figure() − Creates a new figure or activates an existing figure.plt.subplots() − Creates a figure and a set of subplots.Let's take an example to understand the difference between plt.subplots() and plt.figure().StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure. Use plt.figure() method.Create a figure and a set of subplots. Use plt.subplots() method.To display the figure, use Show() method.Examplefrom matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create a new figure using plt.figure fig1 = plt.figure("Figure 1 ...

Read More

Matplotlib – How to show the coordinates of a point upon mouse click?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Oct-2021 9K+ Views

To create a custom mouse cursor in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Bind the function *mouse_event* to the event *button_press_event*.Create x and y data points using numpy.Plot the x and y data points 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.00, 3.50] plt.rcParams["figure.autolayout"] = True def mouse_event(event):     print('x: {} and y: {}'.format(event.xdata, event.ydata)) fig = plt.figure() cid = fig.canvas.mpl_connect('button_press_event', mouse_event) ...

Read More

How to read an input image and print it into an array in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Oct-2021 1K+ Views

To read an input image and print it into an array in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Read an image from a file into an array. Use plt.imread() method.Print the Numpy array of the image.To turn off the axis, use axis('off') method.To display the figure, use Show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True im = plt.imread("forest.jpg") print("Numpy array of the image is: ", im) im = plt.imshow(im) plt.axis('off') plt.show()OutputIt will produce the following output −On the ...

Read More

How to create minor ticks for a polar plot in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Oct-2021 1K+ Views

To create minor ticks for a polar plot in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create r (radius) and theta data points using numpy.Add a subplot to the current figure.Iterate the points between 0 to 360 with step=10 and plot them to get the ticks.To display the figure, use Show() method.Exampleimport numpy as np import matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # radius and theta for the polar plot r = np.arange(0, 5, 0.1) theta = 2 ...

Read More

How to plot an animated image matrix in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Oct-2021 2K+ Views

To plot an animated image matrix in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make an animation by repeatedly calling a function *update*.Inside the update method, create a 6×6 dimension of matrix and display the data as an image, i.e., on a 2D regular raster.Turn off the axes using set_axis_off().To display the figure, use Show() method.Examplefrom matplotlib.animation import FuncAnimation import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() def ...

Read More

How to put a title for a curved line in Python Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Oct-2021 673 Views

To put a title for a curved line in Python Matplotlib, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points such that the line would be a curve.Plot the x and y data points.Place a title for the curve plot using plt.title() method.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create x and y data points x = np.linspace(-1, 1, 50) y = 2**x + 1 # Plot ...

Read More
Showing 101–110 of 902 articles
« Prev 1 9 10 11 12 13 91 Next »
Advertisements