Skip to content Skip to sidebar Skip to footer

How Do I Plot An Energy Ranking Figure Using Python?

This is a typical energy ranking that is used in a few published papers and I am struggling to reproduce one for my data using Python (anything matplotlib, sns etc.). I have my dat

Solution 1:

I implemented an example of what I suggested in the comments. This doesn't automatically scale the axes (I hardcoded it), or add the tick labels, but those are all things you should be able to find on other questions.

import matplotlib.pyplot as plt
import numpy as np

def energy_rank(data, marker_width=.5, color='blue'):
    y_data = np.repeat(data, 2)
    x_data = np.empty_like(y_data)
    x_data[0::2] = np.arange(1, len(data)+1) - (marker_width/2)
    x_data[1::2] = np.arange(1, len(data)+1) + (marker_width/2)
    lines = []
    lines.append(plt.Line2D(x_data, y_data, lw=1, linestyle='dashed', color=color))
    for x in range(0,len(data)*2, 2):
        lines.append(plt.Line2D(x_data[x:x+2], y_data[x:x+2], lw=2, linestyle='solid', color=color))
    return lines

data = np.random.rand(4,8) * 4 # 4 lines with 8 datapoints from 0 - 4

artists = []
for row, color in zip(data, ('red','blue','green','magenta')):
    artists.extend(energy_rank(row, color=color))

fig, ax = plt.subplots()

for artist in artists:
    ax.add_artist(artist)
ax.set_ybound([0,4])
ax.set_xbound([.5,8.5])

Solution 2:

I'm not sure this is technically an answer to this question but I developed a tkinter-based Python application for plotting these graphs in a straightforward manner.

I think it may be useful, please check it out at: https://github.com/ricalmang/mechaSVG

Here is how the user interface looks like: Here is how the user interface looks like

Here is how a typical output graph looks like: Here is how a typical output graph looks like

Technical Note: Under the hood, the procedural generation of the corresponding SVG code (generating element by element) is used for the graph preparation. Most of this is done via a class named SvgGenEsp that won't be detailed here simply because it is too lengthy (300+ lines of code).

Post a Comment for "How Do I Plot An Energy Ranking Figure Using Python?"