如何使用Python绘制能量排名图? [英] How do I plot an energy ranking figure using Python?

查看:39
本文介绍了如何使用Python绘制能量排名图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一些已发表论文中使用的典型能量排名,我正在努力使用 Python(任何 matplotlib、sns 等)为我的数据重现一个.我在 Pandas 数据框中构建了我的数据,如下所示:

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 data constructed in Pandas dataframe like below:

System  Color_id  Data1  Data2
Form I     1      0.0    0.6
Form II    2      0.8    0.0
Form III   3      2.1    2.7
Form IV    4      2.2    0.3
Form V     5      1.7    1.2

我添加了Color_id"列以启用matplotlib中的specific_column_values着色,我想我只是简单地使用plt.scatter进行了一半.我最苦恼的是绘制拉长的线而不是典型的标记,以及如何在不同数据的相似系统之间自动连接那些虚线?非常感谢任何帮助或建议.

I added "Color_id" column to enable specific_column_values coloring as in matplotlib and I think I am half-way using simply plt.scatter. What I am struggling mostly is plotting elongated lines instead of typical markers, and how to automatically connect those dash lines between similar systems across different data? Any help or suggestions is much appreciated.

推荐答案

我实现了我在评论中建议的示例.这不会自动缩放轴(我对其进行了硬编码)或添加刻度标签,但这些都是您应该能够在其他问题中找到的内容.

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])

这篇关于如何使用Python绘制能量排名图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆