Found 10476 Articles for Python

How do I specify an arrow-like linestyle in Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:33:07

1K+ Views

To draw an arrow-like linestyle in matplotlib, we can use quiver() method.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Use quiver() method to draw a line.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-5, 5, 100) y = np.sin(x) plt.quiver(x[:-1], y[:-1], x[1:]-x[:-1], y[1:]-y[:-1], scale_units='xy', angles='xy', scale=1, color='red') plt.show()Output

Plot a vector field over the axes in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:31:57

822 Views

To plot a vector field over the axes in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make X, Y, T, R, U and V data points using numpy.Add an axes to the current figure and make it the current axes.Plot a 3D field of arrows using quiver() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True n = 8 X, Y = np.mgrid[0:n, 0:n] T = np.arctan2(Y - n / 2., X - n/2.) ... Read More

How to plot two Pandas time series on the same plot with legends and secondary Y-axis in Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:30:43

2K+ Views

To plot two Pandas time series on the sameplot with legends and secondary Y-axis, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a one-dimensional ndarray with axis labels (including time series).Make a dataframe with some column list.Plot columns A and B using dataframe plot() method.Return the handles and labels for the legend using get_legend_handles_labels() method.Place a legend on the figure using legend() method.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = ... Read More

How does Python's Matplotlib.pyplot.quiver exactly work?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:29:09

238 Views

To work with quiver, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create vector cordinates using numpy array.Get x, y, u and v data points.Create a new figure or activate an existing figure using figure() method.Get the current axis using gca() method.Set x and y limit of the axes.To redraw the current figure, use draw() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True soa = np.array([[0, 0, 3, 2], [0, 0, 4, 5], [0, 0, ... Read More

How to repress scientific notation in factorplot Y-axis in Seaborn / Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:27:44

6K+ Views

To repress scientific notation in factorplot Y-axis in Seaborn/Matplotlib, we can use style="plain" in ticklabel_format()method.StepsSet the figure size and adjust the padding between and around the subplots.Make a dataframe with keys, col1 and col2.The factorplot() has been renamed to catplot().To repress the scientific notation, use style="plain" in ticklabel_format() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({"col1": [1, 3, 5, 7, 1], "col2": [1, 5, 7, 9, 1]}) sns.catplot(y="col1", x="col2", kind='bar', data=df, label="Total", height=3.5) plt.ticklabel_format(style='plain', ... Read More

How to make axes transparent in Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:26:21

3K+ Views

To make axes transparent in 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 using figure() method.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Set face color of the current axes.Add an axes to the figure.Create t and s data using numpy.Plot t and s data points using plot() method on axis 2 (from step 5).To make the axis transparent, use set_alpha() method and keep alpha value minimum.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import ... Read More

How to name different lines in the same plot of Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:25:14

1K+ Views

To name different lines in the same plot of matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make two lists of data points.Plot point1 and point2 using plot() method.Place a legend on the figure.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True points1 = [2, 4, 1, 5, 1] points2 = [3, 2, 0, 4, 3] plt.plot(points1, 'g--', label="plot A") plt.plot(points2, 'r-o', label="plot A") plt.legend() plt.show()Output

Drawing circles on an image with Matplotlib and NumPy

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:24:09

2K+ Views

To draw a circle on an image with matplotlib and numpy, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Read an image from a file into an array.Create x and y data points using numpy.Create a figure and a set of subplots using subplots() method.Display data as an image, i.e., on a 2D regular raster using imshow() method.Turn off the axes.Add patches on the current axes.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np from matplotlib.patches import Circle plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = ... Read More

How to change the legend fontname in Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:22:28

739 Views

To change the legend fontname in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x data points using numpy.Plot x, sin(x) and cos(x) using plot() method.Use legend() method to place the legend.Iterate legend.get_texts() and update the legend fontname.To display the figure, use 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 x = np.linspace(-5, 5, 100) plt.plot(x, np.sin(x), label="$y=sin(x)$") plt.plot(x, np.cos(x), label="$y=cos(x)$") legend = plt.legend(loc='upper right') i = 1 for t in legend.get_texts():   ... Read More

Adding units to heatmap annotation in Seaborn

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:21:11

655 Views

To add units to a heatmap annotation in Seaborn, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a 5×5 dimension matrix using numpy.Plot rectangular data as a color-encoded matrix.Annotate heatmap value with %age unit.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import seaborn as sns import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(5, 5) ax = sns.heatmap(data, annot=True, fmt='.1f', square=1, linewidth=1.) for t in ax.texts: t.set_text(t.get_text() + " %") plt.show()OutputRead More

Advertisements