Logscale Plots with Zero Values in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:45:00

2K+ Views

To logscale plots with zero values in matplotlib, we can use xscale() and yscale() methods with "symlog" class by name.StepsSet the figure size and adjust the padding between and around the subplots.Plot two lists containing zero values using plot() method.Use yscale() method with "symlog" class by name.Use xscale() method with "symlog" class by name.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.plot([0, 1, 2, 0, 3], [1, 0, 2, 3, 5], marker='o', linestyle='-') plt.yscale('symlog') plt.xscale('symlog') plt.show()Output

Create a Legend for a 3D Bar in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:44:44

1K+ Views

To create a legend for a 3D bar in matplotlib, we can plot 3D bars and place a legend using legend() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an esxisting figure using figure() method.Add an axes to the figure as part of a subplot arrangement.Create a list of data x3, y3, z3, dx, dy and dz using numpy.Plot a 3D bar using bar3d() method.Create a rectangle axis for legend placement.Use legend() method to place the legend for bars.To display the figure, use show() method.Exampleimport numpy as np from matplotlib ... Read More

Plot Dashed Line on Seaborn Lineplot in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:44:18

5K+ Views

To plot a dashed line on a Seaborn lineplot, we can use linestyle="dashed" in the argument of lineplot().StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Use lineplot() method with x and y data points in the argument and linestyle="dashed".To display the figure, use show() method.Exampleimport seaborn as sns import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.rand(10) y = np.random.rand(10) ax = sns.lineplot(x=x, y=y, linestyle="dashed") plt.show()OutputRead More

Skip Empty Dates and Weekends in Financial Matplotlib Python Graph

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:44:01

1K+ Views

To skip weekends in a financial graph in matplotlib, we can iterate the time in dataframe and skip the plot if weekday is 5 or 6.StepsSet the figure size and adjust the padding between and around the subplots.Create a dataframe with keys time.Iterate zipped index and time of a date frame.If iterated timestamp is having weekday 5 or 6, don't plot them.Other than 5 or 6 weekday, plot the points.Set the current tick locations of Y-axis.Lay out a plot with grid lines.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, ... Read More

Write Annotation Outside the Drawing in Data Coords in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:43:44

1K+ Views

We can use annotate() method to place annotation outside the drawing.StepsSet 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 using subplots() method.Use scatter() method to plot x and y data points using star marker and copper color map.To place annotation outside the drawing, use xy coordinates tuple accordingly.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.random.rand(100) y = np.random.rand(100) fig, ax = plt.subplots() ax.scatter(x, y, ... Read More

Histogram for Discrete Values with Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:43:20

3K+ Views

To plot a histogram for discrete values with matplotlib, we can use hist() method.StepsSet the figure size and adjust the padding between and around the subplots.Make a list of discrete values.Use hist() method to plot data with bins=length of data and edgecolor=black.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = [1, 4, 2, 3, 5, 9, 6, 7] plt.hist(data, bins=len(data), edgecolor='black') plt.show()Output

Plotting Upper and Lower Triangle of a Heatmap in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:43:04

1K+ Views

To plot only the upper/lower triangle of a heatmap in matplotlib, we can use numpy to get the masked 2D array and convert them into an image to produce a heatmap.StepsSet the figure size and adjust the padding between and around the subplots.Create a random data of 5×5 dimension.Use numpy.tri() method to create an array with 1's at and below the given diagonal and 0's elsewhere.Get the masked 2D array data with masked array (Using step 3).Use imshow() method to display the data as an image, i.e., on a 2D regular raster.To display the figure, use show() method.Exampleimport numpy as ... Read More

Control Matplotlib Marker Orientation

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:42:45

607 Views

To control matplotlib marker orientation, we can use marker tuple that contains a number of sides, style and rotation or orientation of the marker.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Make an array of 10 different rotations.Zip x, y and i. Iterate them and plot the points using plot() method with marker tuple.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.random.rand(10) y = np.random.rand(10) i = np.linspace(0, 10, 10) for x, ... Read More

Plot Emoji as Label for Bar in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:41:01

2K+ Views

We can use annotate() to place an emoji at the top of a bar.StepsSet the figure size and adjust the padding between and around the subplots.Make a list of frequencies and labels conatining emojis.Create a new figure or activate an existing figure using figure() method.Plot bars using bar() method.Use annotate() method to place emojis as a labelTo 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 freqs = [7, 8, 5, 3, 6] labels = ['😊', '😲', '😂', '😃', '😛'] plt.figure() p1 = plt.bar(np.arange(len(labels)), freqs) for rect1, ... Read More

Add Border to Line in Matplotlib Plot Function

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:40:45

2K+ Views

To give a border to a line in matplotlib plot function, we can call plot() function twice with varying line width.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points where line width=10 and color=black.Again plot x and y points where line width=8 and color=red.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(-2, 2, 100) y = np.sin(x) plt.plot(x, y, c='black', lw=10) plt.plot(x, y, c='red', lw=8) plt.show()OutputRead More

Advertisements