为 seaborn 带状图中的点添加标签
我在 seaborn 上使用条形图来显示一系列金属中心中 d 轨道的能量.
I'm using stripplot on seaborn to show energies of d orbitals in a series of metallic centers.
这是数据框:
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
并且使用此代码我非常接近我想要的(结果如下图):
And using this code I'm quite close to what I want (resulting image below):
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 等)作为标签添加到每个点(或本例中的线哈哈)的右侧(或最佳位置).
I'd like to add the name of each orbital (dxy, dxz etc), as a label, to the right (or the best possible position) on each point (or line in this case haha).
非常感谢任何帮助.
P.S.:我可以看到最后生成的图形在 Y 轴上缺少部分数字.为什么?
P.S.: I can see that the figure generated at the end is missing part of the numbers in the Y axis. Why?
我正在尝试@ImportanceOfBeingErnest 提出的解决方案的seaborn 版本.仅使用一个标记进行测试,目前,我得到了这个 AttributeError: 'NoneType' object has no attribute 'update'
带有很长的回溯,对我来说没有多大意义.这是我的代码:
I'm giving a try to a seaborn version of the solution proposed by @ImportanceOfBeingErnest. Testing with only one of the markers, for now, I'm getting this AttributeError: 'NoneType' object has no attribute 'update'
with a long traceback that doesn't make much sense to me. Here is my code:
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')
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) 坐标中添加或减去数字
upto you how you want to add or subtract numbers in (x,y) coordinates
数据:(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')
您的代码有问题:
在你更新的代码中 dxy.text(df.index[line], df.dxy[line], "teste", horizontalalignment='right', size='medium', color='black')
这里 x=df.index[line]
这是字符串,它应该是数字,因为它是文本的坐标
here x=df.index[line]
which is string, it should be numeric because its a coordinate for your text
第二种方法:
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')
输出: