Python Scatter Plot with Multiple Y Values for Each X

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:52:49

8K+ Views

To make a scatter plot with multiple Y values for each X, we can create x and y data points using numpy, zip and iterate them together to create the scatter plot.StepsSet the figure size and adjust the padding between and around the subplots.Create random xs and ys data points using numpy.Zip xs and ys. Iterate them together.Make a scatter plot with each x and y values.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 xs = np.random.rand(100) ys = np.random.rand(100) for x, y in zip(xs, ... Read More

What Does Axes Flat in Matplotlib Do

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:47:53

5K+ Views

Axes.flat means a 1D iterator over the array. Let's take an example to see how to use axes.flat.StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots using subplots() method.Create x and y data points using numpy.Use axes.flat and iterate all the axes (step 2).Plot x and y data points using plot() 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"] = True fig, axes = plt.subplots(nrows=2, ncols=3) x = np.random.rand(10) y = np.random.rand(10) for _, ax ... Read More

Write Text Above Bars on a Bar Plot in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:47:36

4K+ Views

To write text above the bars on a bar plot, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create lists of year, population and x. Initialize a width variable.Create a figure and a set of subplots using subplots() method.Set ylabels, title, xtickas and xticklabels.Plot the bars using bar() method with x, population and width data.Iterate the bar patches and place text at the top of the bars using text() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = ... Read More

Plot Horizontal and Vertical Lines Through Intersection Point in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:47:19

2K+ Views

To plot horizontal and vertical lines passing through a point, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create two lines using slopes (m1, m2) and intercepts (c1 and c2). Initialize the slopes and intercepts values.Create x data points using numpy.Plot x, m1, m2, c2 and c1 data points using plot() method.Using intercepts and slopes values, find the point of intersection.Plot horizontal and vertical lines with dotted linestyle.Plot xi and yi points on the plotTo display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, ... Read More

Plot 3D Bars Without Axes in Matplotlib

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

446 Views

To plot 3D bars without axes, we can take the following stepsSet 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 to the cureent figure as a subplot arrangement.Create x3, y3 and z3 data points using numpy.Create dx, dy and dz data points using numpy.Use bar3d() method to plot 3D bars.To hide the axes, use axis('off') class by name.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 fig = plt.figure() ... Read More

Plot Overlapping Lines in Matplotlib

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

14K+ Views

To plot overlapping lines in matplotlib, we can use variable overlapping that basically sets the opacity or alpha value in the plot.StepsSet the figure size and adjust the padding between and around the subplots.Initialize a variable overlapping to set the alpha value of the line.Plot line1 and line2 with red and green colors, respectively, with the same alpha value.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 overlapping = 0.150 line1 = plt.plot([1, 3, 5, 2, 5, 3, 1], c='red', alpha=overlapping, lw=5) line2 = plt.plot([7, 2, 5, 7, 5, 2, ... Read More

Disable Minor Ticks of a Log Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:46:16

3K+ Views

To disable the minor ticks of a log plot in matplotlib, we can use minorticks_off() method.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Add a subplot to the current figure, at index 1.Plot x and y data points with color=red.Make x-scale as log class by name.Set the title of the current plot.Add a subplot to the current figure, at index 2.Plot x and y data points with color=green.Make x-scale as log class by name.Turn off the minor ticks of the plot.Set the title of the plot as index 2.To ... Read More

Plotting Distance Arrows in Technical Drawing using Matplotlib

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

1K+ Views

To plot distance arrows in technical drawing in matplotlib, we can use annotate() method with arrow properties.StepsSet the figure size and adjust the padding between and around the subplots.Add a horizontal line across the axis using axhline() method, i.e., y=3.5.Add a horizontal line across the axis using axhline() method, i.e., y=2.5.Use annotate() method to draw an arrow line to show the distance and in the very next statement, use annotate() method again to display the distance between two horizontal lines.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.axhline(3.5) plt.axhline(2.5) ... Read More

Set Same Axis Limits for All Subplots in Matplotlib

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

17K+ Views

To set the same axis limits for all subplots in matplotlib we can use subplot() method to create 4 subplots where nrows=2, ncols=2 having share of x and y axes.StepsSet the figure size and adjust the padding between and around the subplots.Add a subplot to the current figure at index 1.Set the x and y axes view limit using set_xlim() and set_ylim() methods.Plot a line on axis 1 (step 2).Add a subplot to the current figure at index 2 with the same limit (step 3).Plot a line on axis 2.Add a subplot to the current figure at index 3 with ... Read More

Plot Scatter Points on a 3D Projection with Varying Marker Size in Matplotlib

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

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

Advertisements