Found 10476 Articles for Python

How to handle an asymptote/discontinuity with Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 11:48:28

1K+ Views

To handle an asymptote/discontinuity with matplotlib, we can take the following steps −Create x and y data points using numpy.Turn off the axes plot.Plot the line with x and y data points.Add a horizontal line across the axis, x=0.Add a vertical line across the axis, y=0.Place legend for the curve y=1/x.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-1, 1, 100) y = 1 / x plt.axis('off') plt.plot(x, y, label='y=1/x') plt.axhline(y=0, c='red') plt.axvline(x=0, c='red') plt.legend(loc='upper left') plt.show()OutputRead More

How to add a variable to Python plt.title?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:46:22

12K+ Views

To add a varaible to Python plt.title(), we can take the following steps −Create data points for x and y using numpy and num (is a variable) to calculate y and set this in title.Plot x and y data points using plot() method with red color.Set the title of the curve with variable num.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-1, 1, 10) num = 2 y = num ** x plt.plot(x, y, c='red') plt.title(f"y=%d$^x$" % num) plt.show()OutputRead More

How do I apply some function to a Python meshgrid?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:25:51

595 Views

Meshgrid − Coordinate matrices from coordinate vectors.Let's take an example to see how we can apply a function to a Python meshgrid. We can consider two lists, x and y, using numpy vectorized decorator.Exampleimport numpy as np @np.vectorize def foo(a, b):    return a + b x = [0.0, 0.5, 1.0] y = [0.0, 1.0, 8.0] print("Function Output: ", foo(x, y))OutputFunction Output: [0. 1.5 9. ]

How can I plot a single point in Matplotlib Python?

Rishikesh Kumar Rishi
Updated on 23-Aug-2023 14:04:08

68K+ Views

To plot a single data point in matplotlib, we can take the following steps −Initialize a list for x and y with a single value.Limit X and Y axis range for 0 to 5.Lay out a grid in the current line style.Plot x and y using plot() method with marker="o", markeredgecolor="red", markerfacecolor="green".To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [4] y = [3] plt.xlim(0, 5) plt.ylim(0, 5) plt.grid() plt.plot(x, y, marker="o", markersize=20, markeredgecolor="red", markerfacecolor="green") plt.show()OutputRead More

How to normalize a histogram in Python?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:55:40

18K+ Views

To normalize a histogram in Python, we can use hist() method. In normalized bar, the area underneath the plot should be 1.StepsMake a list of numbers.Plot a histogram with density=True.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True k = [5, 5, 5, 5] x, bins, p = plt.hist(k, density=True) plt.show()Output

How to plot a 3D density map in Python with Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:54:49

2K+ Views

To plot a 3D density map in Python with matplotlib, we can take the following steps −Create side, x, y and z using numpy. Numpy linspace helps to create data between two points based on a third number.Return the coordinate matrices from coordinate vectors using side data.Create exponential data using x and y (Step 2).Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, cm, colors import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True side = np.linspace(-2, 2, 15) X, Y = np.meshgrid(side, side) Z = ... Read More

How to show an Axes Subplot in Python?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:03:30

2K+ Views

To show an axes subplot in Python, we can use show() method. When multiple figures are created, then those images are displayed using show() method.StepsCreate x and y data points using numpy.Plot x and y using plot() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(10) y = np.exp(x) plt.plot(x, y) plt.show()Output

Drawing multiple figures in parallel in Python with Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:57:56

998 Views

To draw multiple figures in parallel in Python with matplolib, we can take the following steps−Create random data using numpy.Add a subplot to the current figure, nrows=1, ncols=4 and at index=1.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="Blues_r".Add a subplot to the current figure, nrows=1, ncols=4 and at index=2.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="Accent_r".Add a subplot to the current figure, nrows=1, ncols=4 and at index=3.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="terrain_r"Add a subplot ... Read More

Extract csv file specific columns to list in Python

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:45:13

16K+ Views

To extract csv file for specific columns to list in Python, we can use Pandas read_csv() method.StepsMake a list of columns that have to be extracted.Use read_csv() method to extract the csv file into data frame.Print the exracted data.Plot the data frame using plot() method.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True columns = ["Name", "Marks"] df = pd.read_csv("input.csv", usecols=columns) print("Contents in csv file:", df) plt.plot(df.Name, df.Marks) plt.show()The csv file contains the following data −NameMarksArun98Shyam75Govind54Javed92Raju87OutputWhen we execute the code, it will extract the data from the csv file ... Read More

How to make a log histogram in Python?

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:44:51

4K+ Views

To make a log histogram, we can use log=True in the argument of the hist() method.StepsMake a list of numbers.Plot a histogram with density=True.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 k = np.array([5, 5, 5, 5]) x, bins, p = plt.hist(np.log(k), density=True, log=True) plt.show()Output

Advertisements