我想知道,如果您可以使用列標題作為標簽自動注釋此示例中的每個圖表。
import seaborn as sns
import pandas as pd
d = {'a': [100, 125, 300, 520],..., 'z': [250, 270, 278, 248]}
df = pd.DataFrame(data=d, index=[25, 26, 26, 30])
a ... z
25 100 ... 250
26 125 ... 270
26 300 ... 278
30 520 ... 248
當我使用此代碼時,我只將列標題作為圖例。但是,我希望標簽直接位于我的圖表旁邊/上方。
sns.lineplot(data=df, dashes=False, estimator=None)
uj5u.com熱心網友回復:
這是你想要的?
ax = sns.lineplot(data=df, dashes=False, estimator=None, legend=False)
for label, pos in df.iloc[0].iteritems():
ax.annotate(label, (df.index[0], pos*1.05), ha='left', va='bottom')
輸出:

uj5u.com熱心網友回復:
就像是:
ax = sns.lineplot(data=df, dashes=False, estimator=None)
for c, l in zip(df.columns, ax.lines):
y = l.get_ydata()
ax.annotate(f'{c}', xy=(1.01,y[-1]), xycoords=('axes fraction', 'data'),
ha='left', va='center', color=l.get_color())

來源:https ://stackoverflow.com/a/62703420/15239951
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447235.html
標籤:Python 熊猫 数据框 matplotlib 海运
上一篇:一張圖中兩只熊貓的并排箱線圖
