MayEncoding
MayEncoding

Reputation: 97

Rearrange the step chart by adding 0 to the beginning and a variable value to the last

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

    # 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"])]
        print(values_list)
        points_axisy = signals_df["Value"]
        print(len(points_axisy))
    
        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"]
            print(data)
            # 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)
            ax[pos].yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
            ax[pos].yaxis.set_tick_params(labelsize=6)
            ax[pos].grid(alpha=0.4)
            i += 1
    
        # plt.show()
    

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 and paint it like in the graph I share below:

Image with freehand changes

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.

Upvotes: 2

Views: 79

Answers (1)

tdy
tdy

Reputation: 41407

Based on the yellow markings:

  1. x should be extended left to -1 and extended right to 27 (len(signals_df) - 1)
  2. y should be 0 on the left and continue with data's last value on the right (data.iloc[-1])

You can prepend/append these values as numpy arrays using hstack():

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

Or as lists:

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)

steps extended left and right

Upvotes: 1

Related Questions