将x和y标签添加到打印

假设我有以下代码,它使用pandas绘制了一些非常简单的东西:

将熊猫作为pd导入
值=[[1,2],[2,5]]
df2=pd.DataFrame(值、列=['Type A'、'Type B'],
索引=['索引1','索引2'])
图(lw=2,colormap='jet',marker='jet',markersize=10,
title='Video streaming辍学(按类别)')

如何轻松设置x和y标签,同时保留使用特定颜色贴图的能力?我注意到pandas DataFrames的plot()包装器没有任何特定的参数

df.plot()函数返回一个matplotlib.axes.AxesSubplot对象。可以在该对象上设置标签

ax=df2.plot(lw=2,colormap='jet',marker='1',markersize=10,title='Video streaming dropout by category')
ax.集合标签(“x标签”)
ax.设置标签(“y标签”)

或者更简洁地说:ax.set(xlabel=“x label”,ylabel=“y label”)

或者,索引x轴标签会自动设置为索引名称(如果有)。因此df2.index.name='x label'也可以工作

发表评论