Plot Thousands of Circles Quickly in Matplotlib

Rishikesh Kumar Rishi
Updated on 22-Sep-2021 06:05:11

2K+ Views

To plot thousands of circles quickly in Matplotlib, we will have to use matplotlib.collections. In this case, we will use CircleCollection.StepsImport the collections package from matplotlib along with pyplot and numpy.Set the figure size and adjust the padding between and around the subplots.Initialize variables "num" for number of small circles and "sizes" for sizes of circles.Create a list of circle patches.Add circle patch artist on the current axis.Set the margins of the axes.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.collections as mc plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True num ... Read More

Plotting Multiple DataFrames Using Pandas Functionality

Rishikesh Kumar Rishi
Updated on 22-Sep-2021 06:02:53

26K+ Views

To plot multiple dataframes using Pandas functionality, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create two Pandas dataframes, df1 and df2, of two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot df1 and df2 using plot() method.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df1 = pd.DataFrame( dict( name=['John', 'James', 'Stephen', 'Kandy'], age=[23, 45, 12, 34] ) ... Read More

Plot Time Series Graph Using Seaborn or Plotly

Rishikesh Kumar Rishi
Updated on 22-Sep-2021 05:58:00

3K+ Views

To plot a time series graph using Seaborn or Plotly, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe, df, to hold a date_time series "time" and another variable data, speed.Make a Seaborn line plot with the data, "time" and "speed"Rotate the tick params by 45.To display the figure, use show() method.Exampleimport seaborn as sns from matplotlib import pyplot as plt import pandas as pd import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame( dict( ... Read More

Draw a Curve Connecting Two Points in Matplotlib

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:38:05

4K+ Views

To draw a curve connecting two points instead of a straight line in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Define a draw_curve() method to make a curve with a mathematical expression.Plot point1 and point2 data points.Plot x and y data points returned from the draw_curve() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True def draw_curve(p1, p2): a = (p2[1] - p1[1]) / (np.cosh(p2[0]) - np.cosh(p1[0])) b ... Read More

Date Manipulation to Show Year Tick Every 12 Months in Matplotlib

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:34:04

3K+ Views

To make matplotlib date manipulation so that the year tick shows up every 12 months, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create d, y, s, years, months, monthsFmt and yearsFmt using Pandas, Numpy and matplotlib dates.Use "%B" in DateFormatter to show full month names.Ue "%Y" in DateFormatter to show years.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Plot "dts" and "s" data points using plot() method.Set minor or major axes locator and formatter. Set minor_locator as months ... Read More

Plot Data from a TXT File Using Matplotlib

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:28:07

5K+ Views

To plot data from .txt file using matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize empty lists for bar_names and bar_heights.Open a sample .txt file in read "r" mode and append to bar's name and height list.Make a bar plot.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 bar_names = [] bar_heights = [] for line in open("test_data.txt", "r"): bar_name, bar_height = line.split() bar_names.append(bar_name) bar_heights.append(bar_height) plt.bar(bar_names, bar_heights) plt.show()"test_data.txt" contains the following data −Javed ... Read More

Correlation Between Two Numeric Columns in a Pandas DataFrame

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:10:41

1K+ Views

We can use pandas.DataFrame.corr to compute pairwise correlation of columns, excluding NULL values. The correlation coefficient indicates the strength of the linear association between two variables. The coefficient ranges between -1 and 1.To get the correlation between two numeric columns in a Pandas dataframe, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe of two-dimensional, size-mutable, potentially heterogeneous tabular data.Compare the values of the two columns and compute the correlation coefficient using col1.corr(col2).Print the correlation coefficient on the console.To display the figure, use show() method.Exampleimport pandas as ... Read More

Plot 2D FEM Results Using Matplotlib

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:05:38

886 Views

The Finite Element Method (FEM) is used in a variety of tasks such as modeling of different material types, testing complex geometries, visualizing the local effects acting on a small area of a design. It basically breaks a large spatial domain into simple parts called "finite elements". The simple equations that model these finite elements are then collected into a larger system of equations to model the entire domain.To plot 2d FEM results using matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create nodes, elements and node values data ... Read More

Legend with Vertical Line in Matplotlib

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:00:32

2K+ Views

To add a legend with vertical line 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.Plot the vertical line with red color.The line can have both a solid linestyle connecting all the vertices, and a marker at each vertex.Place a legend on the plot with vertical line.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from matplotlib import lines plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() color = 'red' ax.plot([0, 0], [0, 3], ... Read More

Exponentially Scale the Y-Axis with Matplotlib

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 10:56:42

5K+ Views

To exponentially scale the Y-axis with matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Inintialize a variable dt for steps.Create x and y data points using numpy.Plot the x and y data points using numpy.Set the exponential scale for the Y-axis, using plt.yscale('symlog').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 dt = 0.01 x = np.arange(-50.0, 50.0, dt) y = np.arange(0, 100.0, dt) plt.plot(x, y) plt.yscale('symlog') plt.show()OutputIt will produce the following ... Read More

Advertisements