1

I'm trying to compare a pair of two paired lineplot to visualise how the trend change if the original value are aggregated (by mean or something else).

So basically I have an original dataframe like this (but longer, with about 500 rows):

names measure_1 measure_2 group measure_1_g measure_2_g
name1 2 3 The_first_group 5 7
name2 5 7 The_first_group 5 7
name3 3 4 The_first_group 5 7
name4 8 3 The_second_group 9 5
name5 10 7 The_second_group 9 5

I have tried multiple approaches with matplotlib like:

fig=plt.figure(figsize=(90,10))

ax=fig.add_subplot(111, label="1")
ax2=fig.add_subplot(111, label="2", frame_on=False)

ax.margins(x=0.02)
ax.plot( 'names', 'measure_1', data=df, marker='o', markerfacecolor='xkcd:orange', markersize=7, color='xkcd:orange', linewidth=3, label='measure 1')
ax.plot( 'names', 'measure_2', data=topology_low_norm, marker='o', markerfacecolor='xkcd:red', markersize=7, color='xkcd:red', linewidth=3, label='measure 2')
ax.set_xlabel("original names", color="xkcd:red")
ax.set_ylabel("y original", color="xkcd:orange")
ax.tick_params(axis='x', colors="xkcd:red", labelrotation=90)
ax.tick_params(axis='y', colors="xkcd:orange")

ax2.margins(x=0.02)
ax2.plot( 'group', 'measure_1_g', data=df, marker='^', markerfacecolor='xkcd:aqua', markersize=8, color='xkcd:aqua', linewidth=3, label='Grouped measure 1')
ax2.plot( 'group', 'measure_2_g', data=df, marker='^', markerfacecolor='xkcd:blue', markersize=8, color='xkcd:blue', linewidth=3, label='Grouped measure 2')
ax2.xaxis.tick_top()
ax2.yaxis.tick_right()
ax2.set_xlabel('Groups', color="xkcd:blue") 
ax2.set_ylabel('y Groups', color="xkcd:aqua")       
ax2.xaxis.set_label_position('top') 
ax2.yaxis.set_label_position('right') 
ax2.tick_params(axis='x', colors="xkcd:blue", labelrotation=90)
ax2.tick_params(axis='y', colors="xkcd:aqua")

handles,labels = [],[]
for ax in fig.axes:
    for h,l in zip(*ax.get_legend_handles_labels()):
        handles.append(h)
        labels.append(l)

plt.legend(handles,labels)

plt.savefig('draft.svg')

plt.show()

The plot it is created but obviously the names and the groups have different scale. For the few first items it is ok but then the markers and lines for the groups measurements are shifted and I need to search manually in the top and bottom x axis to search for name1, name2, name3, and their respective group.

I've tried with no success to plot the data with a multi-index dataframe like the example showed here:

https://stackoverflow.com/a/66121322/14576642

The plot is correctly created but I have some problem:

  • I didn't found a way to split the two x axis in a top one (the first level of the multi-index [the groups]), and a bottom one (the second one [names]).
  • I didn't found a way to rotate the external x axis of 90 degree, the internal one is ok if I add the rotation=90 in the code provided in the other answer.
  • The measures for the group are repeated bar, and If I create a multiindex with 4 levels: group, measure_1_g, measure_2_g, names; then I don't know how to manage the plot to show only one value for the groups that superimpose the content of the group (and not 4 values against 4 values)

Biggest problem: I don't know how to edit the code to adapt it to lineplots. If I mess with the code I obtain many error about the settings and numbers of set_ticks and ticks_labels.

This is an example of my first draft, not the linked one:

The firsts cyan-blue and orange-red peaks correspond to names and groups variable that should be superimposed to compare the individual vs the aggregated values

Ideally the image should be longer because the names x-axis should be follow the spacing of the group x axis.

Any idea?

DrewGecko
  • 11
  • 3

0 Answers0