Found 26504 Articles for Server Side Programming

How to pass arguments to animation.FuncAnimation() in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:54:43

830 Views

To pass arguments to animation.FuncAnimation() for a contour plot in Matplotlib in Python, we can take the following steps −Create a random data of 10☓10 dimension.Create a figure and a set of subplots using subplots() method.Make an animation by repeatedly calling a function *func* using FuncAnimation() classTo update the contour value in the function, we can define a method animate() that can be used in FuncAnimation() class.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randn(800).reshape(10, 10, 8) fig, ax ... Read More

How to export to PDF a graph based on a Pandas dataframe in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:53:23

1K+ Views

To export to PDF a graph based on a Pandas dataframe, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a Pandas dataframe with three columns, col1, col2 and col3.Plot the dataframe using plot() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame([[2, 1, 4], [5, 2, 1], [4, 0, 1]], columns=['col1', 'col2', 'col3']) df.plot() plt.savefig('pd_df.pdf')OutputWhen we execute the code, it will save the following plot in a PDF with the name ... Read More

How to change autopct text color to be white in a pie chart in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:51:20

2K+ Views

To change the autopct text color to be white in a pie chart in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a list of hours, activities, and colors to plot pie chart.Make a list of '.Text' instances for the numeric labels, while making the pie chart.Iterate autotexts and set the color of autotext as white.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 plt.figure() hours = [8, 1, 11, 4] activities = ['sleeping', 'exercise', 'studying', 'working'] ... Read More

How to plot with xgboost.XGBCClassifier.feature_importances_ model? (Matplotlib)

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:47:11

364 Views

To change the size of a plot in xgboost.plot_importance, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Load the data from a csv file.Get x and y data from the loaded dataset.Get the xgboost.XGBCClassifier.feature_importances_ model instance.Fit x and y data into the model.Print the model.Make a bar plot.To display the figure, use show() method.Examplefrom numpy import loadtxt from xgboost import XGBClassifier from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # data.csv contains data like -> 13, 145, 82, 19, 110, 22.2, 0.245, 57, ... Read More

How to automatically annotate the maximum value in a Pyplot?

Rishikesh Kumar Rishi
Updated on 09-Jul-2021 07:34:46

8K+ Views

To annotate the maximum value in a Pyplot, 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.Make a list of x and y data points.Plot x and y data points using numpy.Find the maximum in Y array and position corresponding to that max element in the arrayAnnotate that point with local max.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 fig = plt.figure() ax = fig.add_subplot(111) x ... Read More

How to add third level of ticks in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:40:43

331 Views

To add third level of ticks in Python Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create t and s data points using numpy.Create a figure and a set of subplots.Plot t and s using plot() method.Create a twin Axes sharing the Y-axis.Plot t and s using plot() method, on axis one.Set X-axis tick position.Create majors, minors and third level of ticks value (thirds).Set major and minor ticks locator with majors, minors and third tick values (thirds)Set ticks length using tick_params().Plot a horizontal line with gray color.To display the ... Read More

How can I plot hysteresis threshold in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:39:23

645 Views

To plot hysteresis threshold in Matplotlib, 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.Load some greek coins, Greek coins from Pompeii.Find, high, low, and edges of the images using the sobel filter.Apply hysteresis thresholding to "image".Display the data as an image, i.e., on a 2D regular raster, using imshow() method.Set the titles for the original image and the image with hysteresis threshold.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from skimage import data, filters plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] ... Read More

How to make semilogx and semilogy plots in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:38:03

2K+ Views

To make semilogx and semilogy plots, 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.Scatter and plot x and y data points.Make a plot with log scaling on the X axis.Make a plot with log scaling on the Y axis.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 x = [10, 100, 1000, 10000, 100000] y = [2, 4, 8, 16, 32] fig = plt.figure() plt.scatter(x, y) plt.plot(x, y) ... Read More

Programmatically Stop Interaction for specific Figure in Jupyter notebook

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:36:48

742 Views

To programmatically stop interaction for specific figures in Jupyter notebook, we can use plt.off() to stop any interaction after that.Execute the following commnads sequentially −%matplotlib autoimport matplotlib.pyplot as pltplt.rcParams["figure.figsize"] = [7.50, 3.50]plt.rcParams["figure.autolayout"] = TrueCreate a figure and a set of subplots.Plot the line on the axis (From step 5).Turn off the interaction.To display the figure, use show() method.ExampleIn [1]: %matplotlib auto Using matplotlib backend: Qt5Agg In [2]: import matplotlib.pyplot as plt In [3]: plt.rcParams["figure.figsize"] = [7.50, 3.50]    ...: plt.rcParams["figure.autolayout"] = True In [4]: fig, ax = plt.subplots() In [5]: ax.plot([2, 4, 7, 5, 4, 1]) ... Read More

How to animate 3D plot_surface in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:34:55

2K+ Views

To animate 3D plot_surface in Matplotlib, we can take the following steps−Initialize variables for number of mesh grids (N), frequency per second (fps) to call a function, and frame numbers (frn).Create x, y and z array for a curve.Make a function to make a z-array using lambda function.To pass a function into animation class, make a user-defined function that removes the previous plot and plot a surface using x, y and z-array.Create a new figure or activate an existing figure.Add a subplot arrangement using subplots() method.Set the Z-axis limit using set_zlim() method.Call the animation class to animate the surface plot.To ... Read More

Advertisements