Построение логарифмических функций в Python: методы и примеры

Чтобы построить график логарифмической функции в Python, вы можете использовать различные библиотеки, такие как Matplotlib и NumPy. Вот несколько методов, которые вы можете использовать:

Метод 1: использование Matplotlib и NumPy

import numpy as np
import matplotlib.pyplot as plt
# Define the logarithmic function
def logarithmic_function(x):
    return np.log(x)
# Generate x values
x = np.linspace(0.1, 10, 100)
# Calculate y values using the logarithmic function
y = logarithmic_function(x)
# Plot the graph
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('log(x)')
plt.title('Graph of Logarithmic Function')
plt.grid(True)
plt.show()

Метод 2: использование модуля Math и Matplotlib

import math
import matplotlib.pyplot as plt
# Define the logarithmic function
def logarithmic_function(x):
    return math.log(x)
# Generate x values
x = [i/10 for i in range(1, 101)]
# Calculate y values using the logarithmic function
y = [logarithmic_function(i) for i in x]
# Plot the graph
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('log(x)')
plt.title('Graph of Logarithmic Function')
plt.grid(True)
plt.show()

Метод 3: использование SymPy и Matplotlib

import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
# Define the logarithmic function using SymPy
x = sp.symbols('x')
logarithmic_function = sp.log(x)
# Generate x values
x_vals = np.linspace(0.1, 10, 100)
# Calculate y values using the logarithmic function
y_vals = [sp.N(logarithmic_function.subs(x, val)) for val in x_vals]
# Plot the graph
plt.plot(x_vals, y_vals)
plt.xlabel('x')
plt.ylabel('log(x)')
plt.title('Graph of Logarithmic Function')
plt.grid(True)
plt.show()