Python Articles

Page 703 of 852

Plot scatter points on a 3D projection with varying marker size in Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 5K+ Views

To plot scatter points on a 3D projection with varying marker size, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create xs, ys and zs data points using numpyInitialize a variable 's' for varying size of marker.Create a figure or activate an existing figure using figure() method.Add an axes to the current figure as a subplot arrangement using subplots() method.Plot the xs, ys, and zs data points using scatter() method.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"] = ...

Read More

Logscale plots with zero values in Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 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

Read More

How to create a legend for a 3D bar in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 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

How to plot a dashed line on a Seaborn lineplot in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 6K+ 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()Output

Read More

How to skip empty dates (weekends) in a financial Matplotlib Python graph?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 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

How to write annotation outside the drawing in data coords in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 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
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 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

Read More

Plotting only the upper/lower triangle of a heatmap in Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 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

Is it possible to control Matplotlib marker orientation?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 653 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

How to plot an emoji as a label for a bar in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 01-Jun-2021 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
Showing 7021–7030 of 8,519 articles
« Prev 1 701 702 703 704 705 852 Next »
Advertisements