是否使用Pandasdf列作为图例标签?

68de4m5k  于 2022-11-20  发布在  其他
关注(0)|答案(1)|浏览(135)

绘制PandasDatafarme列时,是否可以使用Dataframe列名作为图例标签,而不是显式指定标签?
示例:

import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(data={'col1': [0, 2, 1, 3], 
                        'col2': [9,7,8,9]}, 
                        index=[0, 1, 2, 3])

f = plt.figure()
ax = f.subplots()
ax.plot(df['col1'], label='col1') # How to not explicitly specify label?
# ax.plot(df['col1']) # This does not produce a legend label
ax.legend()
inb24sb2

inb24sb21#

使用Pandas绘图API:

fig, ax = plt.subplots()
df['col1'].plot(ax=ax)
ax.legend()

相关问题