Found 10476 Articles for Python

Dynamically updating a bar plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:02:49

3K+ Views

To update a bar plot dynamically 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.Make a list of data points and colors.Plot the bars with data and colors, using bar() method.Using FuncAnimation() class, make an animation by repeatedly calling a function, animation, that sets the height of the bar and facecolor of the bars.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import animation as animation, pyplot as plt, cm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = ... Read More

How to draw more type of lines in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:02:02

110 Views

To draw more type of lines 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.Plot x and y data points using plot() method, with an array of dashes.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-10, 10, 100) y = np.sin(x) plt.plot(x, y, dashes=[1, 1, 2, 1, 3], linewidth=7, color='red') plt.show()Output

How to move the legend to outside of a Seaborn scatterplot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 11:00:52

2K+ Views

To move the legend to outside of a Seaborn scatterplot, 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 coulmns, column1, column2 and column3.Draw a scatterplot with possibility of several semantic groupings.To place the legend outside the plot, use bbox_to_anchor in legend() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import pandas as pd import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(col1=[2, 1, 4],                       ... Read More

How to change the marker size with pandas.plot() in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:59:32

3K+ Views

To change the marker size with pandas.plot(), we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with three columns, col1, col2 and col3.Use pandas.plot() with marker="*" and markersize=15.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(marker="*", markersize=15) plt.show()Output

How to set timeout to pyplot.show() in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Jul-2021 10:55:41

1K+ Views

To set timeout to pyplot.show() in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new backend-specific subclass of '.Timer'.Add a callback function that will be called whenever one of the plt.close() properties changes.Plot a list of data points.Start the timer.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 fig = plt.figure() # set the timer interval 5000 milliseconds timer = fig.canvas.new_timer(interval = 5000) timer.add_callback(plt.close) plt.plot([1, 2, 3, 4, 5]) plt.ylabel('Y-axis Data') timer.start() plt.show()OutputThe ... Read More

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

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

833 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

368 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

Advertisements