Found 1034 Articles for Matplotlib

How to change the curve using radiobuttons in Matplotlib?

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 08:21:36

295 Views

To change the color of a line using radiobuttons, we can take the following steps −Create x, sin and cos data points using numpy.Adjust the figure size and padding between and around the subplots.Create a figure and a set of subplots using the subplots() method.Plot curve with x and y data points using the plot() method.Add an axes to the current figure and make it the current axes, using the axes() method.Add a radio button to the current axes.To change the curve with radionbutton, we can use the change_curve() method that can be passed in on_clicked() method.To display the figure, use the show() method.Exampleimport numpy ... Read More

Pythonic way of detecting outliers in one dimensional observation data using Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 08:19:37

99 Views

To detect outliers in one dimensional observation data, we can take the following Steps −Create spread, center, flier_high and flier_low.Using the above data (Step 1), we can calculate data.Use the suplots() method to create a figure and a set of subplots, i.e., fig1 and ax1.Set the title of ax1.Now using the boxplot() method and data, make a box and a whisker plot. Beyond the whiskers, data are considered outliers and are plotted as individual points.To display the figure, use the 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 np.random.seed(19680801) spread ... Read More

How to plot the lines first and points last in Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 11:40:58

361 Views

To plot the lines first and points last, we can take the following Steps −Create xpoints, y1points and y2points using numpy, to draw lines.Plot the curves using the plot() method with x, y1 and y2 points.Draw the scatter points using the scatter method.To display the figure, use the 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 xpoints = np.linspace(1, 1.5, 10) y1points = np.log(xpoints) y2points = np.exp(xpoints) plt.plot(xpoints, y1points) plt.plot(xpoints, y2points) for i in xpoints:    plt.scatter(i, np.random.randint(10)) plt.show()OutputRead More

How do I format the axis number format to thousands with a comma in Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:47:21

4K+ Views

First, we can make two lists of x and y, where the values will be more than 1000. Then, we can use the ax.yaxis.set_major_formatter method where can pass StrMethodFormatter('{x:, }') method with {x:, } formatter that helps to separate out the 1000 figures from the given set of numbers.StepsMake two lists having numbers greater than 2000.Create fig and ax variables using subplots method, where default nrows and ncols are 1, using subplot() method.Plot line using x and y (from step 1).Set the formatter of the major ticker, using ax.yaxis.set_major_formatter() method, where StrMethodFormatter helps to make 1000 with common, i.e., expression ... Read More

How to overplot a line on a scatter plot in Python?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:48:17

11K+ Views

First, we can create a scatter for different data points using the scatter method, and then, we can plot the lines using the plot method.StepsCreate a new figure, or activate an existing figure with figure size(4, 3), using figure() method.Add an axis to the current figure and make it the current axes, create x using plt.axes().Draw scatter points using scatter() method.Draw line using ax.plot() method.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.To show the plot, use plt.show() method.Exampleimport random import matplotlib.pyplot as plt plt.figure(figsize=(4, 3)) ax = plt.axes() ax.scatter([random.randint(1, 1000) % 50 for i ... Read More

Plot mean and standard deviation in Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:49:54

9K+ Views

First, we can calculate the mean and standard deviation of the input data using Pandas dataframe.Then, we could plot the data using Matplotlib.StepsCreate a list and store it in data.Using Pandas, create a data frame with data (step 1), mean, std.Plot using a dataframe.To show the figure, use plt.show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt data = [-5, 1, 8, 7, 2] df = pd.DataFrame({       'data': data,       'mean': [2.6 for i in range(len(data))],       'std': [4.673328578 for i in range(len(data))]}) df.plot() plt.show()Output

Text box with line wrapping in Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:51:16

1K+ Views

Matplotlib can wrap text automatically, but if it's too long, the text will be displayed slightly outside of the boundaries of the axis anyways.StepsCreate a new figure, or activate an existing figure, using figure().Set the axis properties using plt.axis() method.Make a variable input_text to store the string.Add text to figure, using plt.text() method where style='oblique', ha='center', va='top', ...etc.To show the figure use plt.show() method.Exampleimport matplotlib.pyplot as plt fig = plt.figure() plt.axis([0, 10, 0, 10]) input_text = 'Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy.' plt.text(5, 5, input_text, fontsize=10, style='oblique', ha='center', va='top', ... Read More

Barchart with vertical labels in Python/Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:32:55

7K+ Views

First, we can create bars using plt.bar and using xticks. Then, we can align the labels by setting the “vertical” or “horizontal” attributes in the “rotation” key.StepsMake lists, bars_heights, and bars_label, with numbers.Make a bar plot using bar() method, with bars_heights and length of bars_label.Get or set the current tick locations and labels of the X-axis, using xticks() with rotation='vertical' and bars_label.To show the plot, use plt.show() method.Examplefrom matplotlib import pyplot as plt bars_heights = [14, 8, 10] bars_label = ["A label", "B label", "C label"] plt.bar(range(len(bars_label)), bars_heights) plt.xticks(range(len(bars_label)), bars_label, rotation='vertical') plt.show()OutputRead More

Displaying rotatable 3D plots in IPython or Jupyter Notebook

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:36:34

3K+ Views

By creating a 3D projection on the axis and iterating that axis for different angles using view_init(), we can rotate the output diagram.StepsCreate a new figure, or activate an existing figure.Add an `~.axes.Axes` to the figure as part of a subplot arrangement with nrow = 1, ncols = 1, index = 1, and projection = '3d'.Use the method, get_test_data to return a tuple X, Y, Z with a test dataset.Plot a 3D wireframe with data test data x, y, and z.To make it rotatable, we can set the elevation and azimuth of the axes in degrees (not radians), using view_init() ... Read More

How to print the Y-axis label horizontally in a Matplotlib/Pylab chart?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:42:02

3K+ Views

Just by using plt.ylabel(rotation='horizontal'), we can align a label according to our requirement.StepsPlot the lines using [0, 5] and [0, 5] lists.Set the y-label for Y-axis, using ylabel method by passing rotation='horizontal'.Set the x-label for X-axis, using xlabel method.To show the plot, use plt.show() method.Examplefrom matplotlib import pyplot as plt plt.plot([0, 5], [0, 5]) plt.ylabel("Y-axis ", rotation='horizontal') plt.xlabel("X-axis ") plt.show()Output

Advertisements