You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何针对非均匀变量值批量绘制依赖函数曲线

Solution for Plotting Curves with Non-Uniform k Values

Absolutely! You don’t need to rewrite your function for each non-uniform k value—just store your k values in a list and loop through them to generate and plot each curve on the same figure. Here’s a complete, modified version of your code that does this:

import numpy as np
import matplotlib.pyplot as plt

# Define your non-uniform k values as a list
k_values = [1.9, 2.9, 4.3, 8.2]

# Create your x data (adjust the range/resolution as needed)
x = np.linspace(0, 5, 100)  # Example: 100 points from 0 to 5

# Optional: Define your function as a reusable helper (cleaner code)
def my_function(x, k):
    # Replace this with your actual function logic
    return np.power(x, k)  # Example: y = x^k

# Loop through each k value to compute and plot the curve
for k in k_values:
    y = my_function(x, k)
    # Plot with a label to identify each k in the legend
    plt.plot(x, y, label=f'k = {k}')

# Add plot elements for clarity
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Curves for Non-Uniform k Values')
plt.legend()  # Shows the labels for each k
plt.grid(True)  # Optional: Adds a grid for readability

# Display the plot
plt.show()

Key Notes:

  • List of k Values: Storing your non-uniform k values in k_values lets you easily iterate over them without repeating code.
  • Reusable Function: Defining my_function keeps your code organized—just swap out the example logic (np.power(x, k)) with your actual function that depends on k.
  • Labels & Legend: Adding label=f'k = {k}' to each plt.plot() call ensures you can distinguish between curves in the legend.
  • Single Axes: All plots are drawn on the same figure by default, so you don’t need extra setup for multiple curves.

If your existing function is already written, you can just drop it into the loop directly (instead of using the helper function) if you prefer—this structure just keeps things cleaner for more complex functions.

内容的提问来源于stack exchange,提问作者Brudalaxe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:32:33