[Python] How do I load data from a CSV file using this code? Or other methods?

Discussão em 'Python' iniciado por Stack, Maio 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to get this code to work by using Only CSV data from a CSV file (prices.csv below) is there a way to remove the ABCDE n_bars value and replace it with a CSV version for this code? If anyone has anyway of doing this please demonstrate using comment indicator (#) so I can learn, thanks.

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Slider
    from itertools import product

    n_bars = 100
    n_shown = 10

    names = ["".join(t) for t in product("ABCDE", "ABCDE", "ABCDE")][:n_bars]
    np.random.seed(20250502)
    values = np.random.randint(1, 11, n_bars)

    fig, ax_d = plt.subplot_mosaic("a" * 7 + "b")
    ax_d["a"].barh(names, values)
    ax_d["a"].axis([-0.2, 10.2, -0.5, n_shown-0.5])
    ax_d["a"].invert_yaxis()

    spos = Slider(ax_d["b"], "", -0.5, n_bars-n_shown-0.5, orientation="vertical")
    def update(val):
    pos = spos.val
    ax_d["a"].axis([-0.2, 10.2, pos+n_shown, pos])
    fig.canvas.draw_idle()
    spos.on_changed(update)

    plt.show()

    Prices.csv


    Prices-x1,1
    Prices-x1,At-1
    Prices-x10,1
    Prices-x10,Away-1
    Prices-x1,2
    Prices-x1,At-1
    Prices-x20,1
    Prices-x20,Away-1
    Prices-x5,1
    Prices-x5,Away-2

    Continue reading...

Compartilhe esta Página