Plot Scatter Points with Increasing Size of Marker in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:11:20

762 Views

To plot scatter points with increasing size of marker, we can take the following steps−StepsCreate x and y data pointsTo get increasing size of marker, make a list of numbers.Use scatter method to plot scatter points.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 = [0, 2, 4, 6, 8, 10] y = [0] * len(x) s = [10 * 4 ** n for n in range(len(x))] plt.scatter(x, y, s=s, c='red') plt.show()Output

Style Part of Label in Legend in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:10:46

248 Views

To style a part of label in legend, we can take the following steps −Create data point for x using numpy.Plot a sine curve using np.sin(x) with a text label.Plot a cosine curve using np.cos(x) with a text label.To place the legend on the plot, use legend() 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 x = np.linspace(-1, 1, 10) plt.plot(x, np.sin(x), label="This is $\it{a\ sine\ curve}$") plt.plot(x, np.cos(x), label="This is $\bf{a\ cosine\ curve}$") plt.legend(loc='lower right') plt.show()OutputRead More

Plot Only a Table in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:10:24

6K+ Views

To plot only a table, we can take the following steps−Create fig and axs, using subplots. Create a figure and a set of subplots.Create random data for 10 rows and 3 columns.Create a tuple for columns name.axis('tight') − Set the limits, just large enough to show all the data, then disable further autoscaling.axis('off') − Turn off axis lines and labels. Same as ''False''.To add a table on the axis, use table() instance, with column text, column labels, columns, and location=center.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"] ... Read More

Add Different Graphs as Inset in Another Python Graph

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:06:12

1K+ Views

To add different graphs (as an inset) in another Python graph, 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, i.e., fig and ax.To create a new axis, add axis to the existing figure (Step 2).Plot x and y on the axis (Step 2).Plot x and y on the new axis (Step 3).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 x = np.linspace(-1, 1, 100) y = np.sin(x) fig, ax = plt.subplots() left, bottom, width, height = [.30, 0.6, ... Read More

Change Order of Plots in Pandas hist Command

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:05:53

548 Views

To change order of plots in Pandas hist commad, we can take the following steps −Make a data frame using Pandas.Plot a histogram with the data frame.Plot the data frame in different order.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'a': [1, 1, 1, 1, 3],    'b': [1, 1, 2, 1, 3],    'c': [2, 2, 2, 1, 3], }) df.hist() df[['c']].hist() df[['a']].hist() df[['b']].hist() plt.show()Output

Add Vertical Lines to a Distribution Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:05:00

3K+ Views

To add vertical lines to a distribution plot, we can take the following steps−Create a list of numbers.Create an axis using sns.displot().Get x and y data of the axis ax.Plot a vertical line on the plot.Remove the line at the 0th index.To display the figure, use show() method.Exampleimport seaborn as sns, numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [5, 6, 7, 2, 3, 4, 1, 8, 2] ax = sns.distplot(x, kde=True) x = ax.lines[0].get_xdata() y = ax.lines[0].get_ydata() plt.axvline(x[np.argmax(y)], color='red') ax.lines[0].remove() plt.show()OutputRead More

Draw Log Normalized Imshow Plot with Colorbar in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:04:00

9K+ Views

To draw a log-normalized imshow() plot with a colorbar representing the raw data in matplotlib, we can take the following steps −Create a 2D array using numpy.Display the data as an image, i.e., on a 2D regular raster, using imshow() methodCreate a colorbar for a ScalarMappable instance, *mappable*, using imshow() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, cm from matplotlib import colors plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(4, 4) im = plt.imshow(data, cmap=cm.rainbow, norm=colors.LogNorm()) plt.colorbar(im) plt.show()OutputRead More

Circular Polar Histogram in Python

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:03:31

2K+ Views

To plot circular (polar) histogram in Python, we can take the following steps−Create data points for theta, radii and width using numpy.Add a subplot to the current figure, where projection='polar' and nrows=1, ncols=1 and index=1.. Make a bar plot using bar() method, with theta, radii and width data pointsIterate radii and bars after zipping them together and set the face color of the bar and the alpha value. Lesser the alpha value, greater the transparency.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True N = 20 theta = ... Read More

Put Text Outside Python Plots

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:03:04

3K+ Views

To put text outside a plot, we can change the text position by changing the value of text_pos_x and text_pos_yStepsCreate data points for x and y.Initialize the text position of x and y.To plot x and y, use plot() method with color='red'.Use text() method to add text to figure.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 x = np.linspace(1, 5, 100) y = np.exp(x) text_pos_x = 0.60 text_pos_y = 0.50 plt.plot(x, y, c='red') plt.text(text_pos_x, text_pos_y, "$\mathit{y}=e^{x}$", fontsize=14, transform=plt.gcf().transFigure, color='green') plt.show()OutputRead More

Adjusting Gridlines and Ticks in Matplotlib Imshow

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:02:37

4K+ Views

To adjust gridlines and ticks in matplotlib imshow(), we can take the following steps−Create data, a 2D array, using numpy.Using imshow() method, display data as an image.Set xticks and yticks using set_xticks and set_yticks method.To set the xticklabels and yticklabels, use set_xticklabels and set_yticklabels method.Lay out a grid in current line style. Supply the list of x an y positions using grid() 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 data = np.random.rand(9, 9) plt.imshow(data, interpolation="nearest") ax = plt.gca() ax.set_xticks(np.arange(-.5, 9, 1)) ax.set_yticks(np.arange(-.5, 9, 1)) ... Read More

Advertisements