I'm developing this project in Python using the Tkinter, ElementTree, Numpy, Pandas and Matplotlib packages:

    # Function to extract the Name and Value attributes
    def extract_name_value(signals_df, rootXML):
        # print(signals_df)
        names_list = [name for name in signals_df['Name'].unique()]
        num_names_list = len(names_list)
        num_axisx = len(signals_df["Name"])
        values_list = [value for pos, value in enumerate(signals_df["Value"])]
        points_axisy = signals_df["Value"]
        colors = ['b', 'g', 'r', 'c', 'm', 'y']
        # Creation Graphic
        fig, ax = plt.subplots(nrows=num_names_list, figsize=(20, 30), sharex=True)
        plt.suptitle(f'File XML: {rootXML}', fontsize=16, fontweight='bold', color='SteelBlue', position=(0.75, 0.95))
        plt.xticks(np.arange(-1, num_axisx), color='SteelBlue', fontweight='bold')
        i = 1
        for pos, name in enumerate(names_list):
            # get data
            data = signals_df[signals_df["Name"] == name]["Value"]
            # get color
            j = random.randint(0, len(colors) - 1)
            # get plots by index = pos
            ax[pos].plot(data.index, data, drawstyle='steps-post', marker='o', color=colors[j], linewidth=3)
            ax[pos].set_ylabel(name, fontsize=8, fontweight='bold', color='SteelBlue', rotation=30, labelpad=35)
            i += 1

But I would like to make the y-axis values start at 0 in all subplots() cases and end up to the size or length of the points_axisy variable

That is to say, that the lines painted in yellow freehand, is replaced by the values of the graph but I do not understand how to do it. I've already been testing code with the enumerate function but I can't find the solution. The xml file to test my code can be taken from: xml file Thank you very much in advance for your help, any comments help.


I leave the answer for possible further search.

I just changed the line:

ax[pos].plot(data.index, data, drawstyle='steps-post', marker='o', color=colors[j], linewidth=3)

For this:

x = [-1] + data.index.tolist() + [len(signals_df) - 1]
y = [0] + data.tolist() + [data.iloc[-1]]
ax[pos].plot(x, y, drawstyle='steps-post', marker='o', color=colors[j], linewidth=3)

and it worked. Thank you!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.