在海上条形图中的点上添加标签

时间:2019-03-10 17:32:18

标签: python python-3.x pandas matplotlib seaborn

我正在使用Seaborn上的带状图来显示一系列金属中心中d轨道的能量。

以下是数据框:

        dxy      dyz      dz2      dxz   dx2-y2
Fe -0.25336 -0.24661 -0.22991 -0.07644 -0.16229
Co -0.38294 -0.38050 -0.34952 -0.21271 -0.27173
Ni -0.47550 -0.47504 -0.46817 -0.44385 -0.45632

使用此代码,我已经很接近我想要的了(结果如下图所示):

plt.figure(figsize=(3, 7))
sns.stripplot(x=df.index, y="dxy", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
sns.stripplot(x=df.index, y="dyz", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
sns.stripplot(x=df.index, y="dz2", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
sns.stripplot(x=df.index, y="dxz", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
sns.stripplot(x=df.index, y="dx2-y2", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
plt.ylabel("Energy (Eh)")
plt.savefig('svm_conf.png', dpi=400)

我想将每个轨道的名称(dxy,dxz等)作为标签添加到每个点(在这种情况下为直线)的右侧(或最佳位置)。

非常感谢您的帮助。

P.S .:我可以看到,最后生成的数字在Y轴上缺少部分数字。为什么?

更新

我正在尝试@ImportanceOfBeingErnest提出的解决方案的简化版本。目前,仅使用其中一个标记进行测试,我得到的AttributeError: 'NoneType' object has no attribute 'update'的追溯很长,这对我来说没有太大意义。这是我的代码:

dxy = sns.stripplot(x=df.index, y="dxy", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
for line in range(0, df.shape[0]):
    dxy.text(df.index[line], df.dxy[line], "teste", horizontalalignment='right', size='medium', color='black')

enter image description here

1 个答案:

答案 0 :(得分:1)

    g.text(x=i+0.1, y=df[df.columns[j]].values[i]+0.001, s=df.columns[j], horizontalalignment='right', size='medium', color='black')

关于如何在(x,y)坐标中增加或减少数字

数据:(sam.csv)

d,dxy,dyz,dz2,dxz,dx2-y2
Fe,-0.25336,-0.24661,-0.22991,-0.07644,-0.16229
Co,-0.38294,-0.38050,-0.34952,-0.21271,-0.27173
Ni,-0.47550,-0.47504,-0.46817,-0.44385,-0.45632

代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.lines import Line2D
import random

df = pd.read_csv('sam.csv').reset_index()
df.index = df['d']
del df['d']
print(df.columns)
print(df)
ax = plt.figure(figsize=(10, 7))
colors = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])
             for i in range(len(df.columns) - 1)]
# colors = ['#FFAA11', '#11AA11', '#55AA31', '#11BA81', '#CCABAA']

columns = list(df.columns)
for j in range(1, len(df.columns)):
    color = colors[j-1]
    g = sns.stripplot(x=df.index, y=columns[j], data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2,
                      color=color)

for j in range(1, len(df.columns)):

    for i in range(len(df)):
        g.text(x=i+0.1, y=df[df.columns[j]].values[i]+0.001, s=df.columns[j], horizontalalignment='right', size='medium', color='black')

elements = [Line2D([0], [0], color=colors[i]) for i in range(len(df.columns)-1)]

ax.legend(handles=elements, labels=list(df.columns)[1:])

plt.ylabel("Energy (Eh)")
plt.savefig('svm_conf.png', dpi=400)
plt.show()

输出:

Index(['index', 'dxy', 'dyz', 'dz2', 'dxz', 'dx2-y2'], dtype='object')

enter image description here


您的代码中的问题:

在您更新的代码dxy.text(df.index[line], df.dxy[line], "teste", horizontalalignment='right', size='medium', color='black')

此处x=df.index[line]是字符串,应为数字,因为它是文本的坐标


第二种方法:

for j in range(1, len(df.columns)):
    flag = True
    for i in range(len(df)):
        if flag is True:
            delta = 0.3
            align = 'left'
            flag = False
        else:
            delta = -0.2
            align = 'right'
            flag = True
        g.text(x=i+delta, y=df[df.columns[j]].values[i], s=df.columns[j], horizontalalignment=align, size='medium', color='black')

输出:

enter image description here