Python Forum
function accepts infinite parameters and returns a graph with those values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function accepts infinite parameters and returns a graph with those values
#1
Hi y'all,

I am trying to create a function that accepts infinite parameters and returns one graph with all parameter values (beta) on it. Currently, the function only returns one graph (good), but it will only graph the first parameter (bad). I'm not sure how to make it graph all lines. I thought my current code would be able to at least graph the first two parameters entered (I know that it wouldn't work for more than that), but it didn't work for two parameters.

I guess I don't know how to keep assigning ys to the new parameters (beta). Any help would be great!

def beta_graph(*arg):
"""Accepts infinite parameters- which are the beta values- and returns one graph with
    all beta values on that graph
    
    Note: current code does not do this, but this is the goal"""
    
    # assigning counting variable
    num = 0
    length = len(arg)
    
    # starting with 0th index of arg tuple
    beta = arg[num]
    
    # creating the figure and axes
    fig, ax = plt.subplots(1, 1)
    
    # generating the x-axis data
    xs = np.linspace(1, 5, 1000)

    # assigning constant values
    v_inf = 2700 # km/s
    v_0 = 1 # km/s

    # y-axis data for 0th index
    ys1 = (v_0 + (v_inf - v_0)*(1 - 1/xs)**beta)/v_inf

    # plotting 0th index data
    ax.plot(xs, ys1, label= f'beta = {beta}')
    ax.text(5, beta, f"{beta}", ha='center')
    
    # setting x and y labels
    ax.set_xlabel('$r / R_*$')
    ax.set_ylabel('$v(r) / v_{inf}$')
    
    while True:
        # if we've reached the end of the tuple, exit while loop
        if (num + 1) == length:
            break
        # if we haven't reached the end of the tuple,
        # graph the next beta value
        else:
            # y-axis data for next index
            ys2 = (v_0 + (v_inf - v_0)*(1 - 1/xs)**beta)/v_inf
        
            # plotting data
            ax.plot(xs, ys2, label= f'beta = {beta}')
            ax.text(5, beta, f"{beta}", ha='center')
            
            num = num + 1 

beta_graph(0.1, 0.2)
# I want them to print on the same graph
Output:
   
Larz60+ write Jun-10-2022, 04:48 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please refrain from using images to post code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  cmath.rect accepts a negative modulus JMB 2 325 Jan-17-2024, 08:00 PM
Last Post: JMB
Question How to compare two parameters in a function that has *args? Milan 4 1,253 Mar-26-2023, 07:43 PM
Last Post: Milan
  Adding values with reduce() function from the list of tuples kinimod 10 2,623 Jan-24-2023, 08:22 AM
Last Post: perfringo
  function returns dataframe as list harum 2 1,386 Aug-13-2022, 08:27 PM
Last Post: rob101
  How to do bar graph with positive and negative values different colors? Mark17 1 5,098 Jun-10-2022, 07:38 PM
Last Post: Mark17
  Function - Return multiple values tester_V 10 4,431 Jun-02-2021, 05:34 AM
Last Post: tester_V
  function that returns a list of dictionaries nostradamus64 2 1,737 May-06-2021, 09:58 PM
Last Post: nostradamus64
  How can I write a function with three parameters? MehmetAliKarabulut 1 2,409 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  Parameters aren't seen inside function Sancho_Pansa 8 2,880 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa
  Recursive function returns None, when True is expected akar 0 3,375 Sep-07-2020, 07:58 PM
Last Post: akar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020