Found 1034 Articles for Matplotlib

How to save a histogram plot in Python?

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:13:15

6K+ Views

To save a histogram plot in Python, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create data points "k" for the histogram.Plot the histogram using hist() method.To save the histogram, use plt.savefig('image_name').To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Data points for the histogram k = [1, 3, 2, 5, 4, 7, 5, 1, 0, 4, 1] # Plot the histogram plt.hist(k) # Save the histogram plt.savefig('hist.png') # Display the ... Read More

Correlation between two numeric columns in a Pandas DataFrame

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

948 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

How to plot 2d FEM results using matplotlib?

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

595 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

1K+ 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

How to annotate seaborn pairplots?

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 10:51:15

399 Views

To annotate Seaborn pairplots, we can use the fig.text() method.StepsImport Seaborn, Pandas, Numpy, and Pyplot packages.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.Plot pairwise relationships in a dataset, using sns.pairplot().Add an annotated text using fig.text() method.To display the figure, use show() method.Exampleimport seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame( np.random.random((4, 4)), columns=["a", "b", "c", "d"] ) pp = ... Read More

How to draw the largest polygon from a set of points in matplotlib?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:49:14

456 Views

To draw the largest polygon from a set of points in matplotlib, we can take the following steps −Import "Polygon" from matplotlib.patches.Set the figure size and adjust the padding between and around the subplots.Create a list of data points for the largest polygon.Get the polygon instance.Create a figure and a set of subplots.Add a polygon instance patch.Set the x and y scale limit.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Polygon plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True y = np.array([[1, 1], [0.5, 1.5], [2, 1], [1, 2], [2, ... Read More

How to change the face color of a plot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:32:03

6K+ Views

To change the face color of a plot using 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.Create a figure and a set of subplots.Plot the x and y data points using plot() method with color=yellow and linewidth=7.Set the facecolor of the axes, using set_facecolor().To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create x and y data points x ... Read More

Plotting a masked surface plot using Python, Numpy and Matplotlib

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:23:51

541 Views

To plot a masked surface plot using Python, Numpy and 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.Add an 'ax' to the figure as part of a subplot arrangement.Return the coordinate matrices from coordinate vectors, pi and theta.Create x, y and z with masked data points.Create a surface plot with x, y, and z data points.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 fig = ... Read More

Matplotlib – Drawing lattices and graphs with Networkx

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:19:25

954 Views

To draw lattices and graphs with networkx, we can take the following steps −Import networkx and pyplot.Set the figure size and adjust the padding between and around the subplots.Use nx.grid_2d_graph(3, 3) to get a two-dimensional grid graph. The grid graph has each node connected to its four nearest neighbors.Draw the graph G with Matplotlib.To display the figure, use show() method.Example# Import networkx and pyplot import networkx as nx from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Draw the graph G = nx.grid_2d_graph(3, 3) nx.draw(G, node_size=100) plt.show()OutputIt ... Read More

How to set the border color of the dots in matplotlib's scatterplots?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:07:59

4K+ Views

To set the border color of the dots in matplotlib scatterplots, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable "N" to store the number of sample data.Create x and y data points using numpy.Plot the x and y data points using scatter() method. To set the border color of the dots, use the edgecolors parameter in the scatter() method. Here, we have used "red" as the border color of the dots by using edgecolors='red'.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot ... Read More

Advertisements