Programming Articles - Page 1198 of 3363

What is the necessity of plt.figure() in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:25:30

350 Views

Using plt.figure(), we can create multiple figures and to close them all explicitly, call plt.close(). If you are creating many figures, make sure you explicitly call pyplot.close on the figures you are not using, because this will enable pyplot to properly clean up the memory.Using subplots(), we can create a figure and set of subplots.Here we creating two figures, fig1 and fig2. fig1 is 8×8 in size, whereas fig2 has the default figsize. There are 4×4=16 subplots added in fig2.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig2, ax_lst = plt.subplots(4, 4) plt.show()OutputRead More

How can I plot a single point in Matplotlib Python?

Rishikesh Kumar Rishi
Updated on 23-Aug-2023 14:04:08

69K+ Views

To plot a single data point in matplotlib, we can take the following steps −Initialize a list for x and y with a single value.Limit X and Y axis range for 0 to 5.Lay out a grid in the current line style.Plot x and y using plot() method with marker="o", markeredgecolor="red", markerfacecolor="green".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 x = [4] y = [3] plt.xlim(0, 5) plt.ylim(0, 5) plt.grid() plt.plot(x, y, marker="o", markersize=20, markeredgecolor="red", markerfacecolor="green") plt.show()OutputRead More

How to adjust transparency (alpha) in Seaborn pairplot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:15:49

6K+ Views

To adjust transparency, i.e., aplha in Seaborn pairplot, we can change the value of alpha.StepsCreate a dataframe using Pandas with two keys, col1 and col2.Initialize the variable, alpha, for transparency.Use pairplot() method to plot pairwise relationships in a dataset. Use df (from step 1), kind="scatter", and set the plot size, edgecolor, facecolor, linewidth and alpha vaues in the arguments.To display the figure, use show() method.Exampleimport pandas as pd import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({"col1": [1, 3, 5, 7, 1], "col2": [1, 5, 7, 9, 1]}) alpha = 0.75 ... Read More

How to retrieve XY data from a Matplotlib figure?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:14:50

6K+ Views

To retrieve XY data from a matplotlib figure, we can use get_xdata() and get_ydata() methods.StepsCreate x and y data points using numpy.Limit X and Y axes range, using xlim() and ylim() methods.Plot xs and ys data points using plot() method with marker=diamond, color=red, and markersize=10, store the returned tuple in a line.Use get_xdata() and get_ydata() methods on the line to get xy data.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 xs = np.random.rand(10) ys = np.random.rand(10) plt.xlim(0, 1) plt.ylim(0, 1) line, = plt.plot(xs, ys, marker='d', c='red', markersize=10) xdata = line.get_xdata() ydata = ... Read More

How do I let my Matplotlib plot go beyond the axes?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:11:13

2K+ Views

To let my matplotlib plot go beyond the axes, we can turn off the flag clip_on in the argument of plot() method.StepsCreate xs and ys data points using numpy.Limit the X and Y axis range in the plot to let the line go beyond this limit, using xlim() and ylim() method.Plot the xs and ys data points using plot() method, where marker is a diamond shape, color is orange and clip_on=False (to go beyond the plot).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 xs = np.arange(10) ys ... Read More

How to avoid overlapping of labels & autopct in a Matplotlib pie chart?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:10:51

9K+ Views

To avoid overlapping of labels and autopct in a matplotlib pie chart, we can follow label as a legend, using legend() method.StepsInitialize a variable n=20 to get a number of sections in a pie chart.Create slices and activities using numpy.Create random colors using hexadecimal alphabets, in the range of 20.Use pie() method to plot a pie chart with slices, colors, and slices data points as a label.Make a list of labels (those are overlapped using autopct).Use legend() method to avoid overlapping of labels and autopct.To display the figure, use show() method.Exampleimport random import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = ... Read More

Fixing color in scatter plots in Matplotlib

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:10:30

935 Views

To fix colors in scatter plots in matplotlib, we can take the following steps −Create xs and ys random data points using numpy.Create a set of colors using hexadecimal alpabets, equal to the length of ys.Plot the lists, xs and ys, using scatter() method, with the list of colors.To display the figure, use show() method.Exampleimport random import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xs = np.random.rand(100) ys = np.random.rand(100) colors = ["#" + ''.join([random.choice('0123456789ABCDEF') for j in range(6)]) for i in range(len(xs))] plt.scatter(xs, ys, c=colors) plt.show()OutputRead More

How do I plot two countplot graphs side by side in Seaborn using Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:07:24

3K+ Views

To plot two countplot graphs side by side in Seaborn, we can take the following steps −To create two graphs, we can use nrows=1, ncols=2 with figure size (7, 7).Create a dataframe with keys, col1 and col2, using Pandas.Use countplot() to show the counts of observations in each categorical bin using bars.Adjust the padding between and around the subplots.To display the figure, use show() method.Exampleimport pandas as pd import numpy as np import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True f, axes = plt.subplots(1, 2) df = pd.DataFrame(dict(col1=np.linspace(1, 10, 5), col2=np.linspace(1, ... Read More

Setting Transparency Based on Pixel Values in Matplotlib

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:07:03

1K+ Views

To set transparency based on pixel values in matplotlib, get masked data wherever data is less than certain values. Lesser value will result in full overlapping between two images.StepsCreate data1 and data2 using numpy.Get the masked data using numpy's masked_where() method.Using subplots() method, create a figure and a set of subplots (fig and ax).Display the data (data1 and masked data) as an image, i.e., on a 2D regular raster, using imshow() method, with different colormaps, jet and gray.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data1 = np.random.rand(50, 50) data2 = ... Read More

How can I change the font size of ticks of axes object in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:06:39

619 Views

To change the font size of ticks of axes object in matplotlib, we can take the following steps −Create x and y data points using numpy.Using subplots() method, create a figure and a set of subplots (fig and ax).Plot x and y data points using plot() method, with color=red and linewidth=5.Set xticks with x data points.Get the list of major ticks using get_major_ticks() method.Iterate the major ticks (from step 5), and set the font size and rotate them by 45 degrees.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 ... Read More

Advertisements