在 pandas 图上添加x和y标签 [英] Add x and y labels to a pandas plot

查看:133
本文介绍了在 pandas 图上添加x和y标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suppose I have the following code that plots something very simple using pandas:

import pandas as pd
values = [[1, 2], [2, 5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'], 
                   index=['Index 1', 'Index 2'])
df2.plot(lw=2, colormap='jet', marker='.', markersize=10, 
         title='Video streaming dropout by category')

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

How do I easily set x and y-labels while preserving my ability to use specific colormaps? I noticed that the plot() wrapper for pandas DataFrames doesn't take any parameters specific for that.

推荐答案

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

The df.plot() function returns a matplotlib.axes.AxesSubplot object. You can set the labels on that object.

ax = df2.plot(lw=2, colormap='jet', marker='.', markersize=10, title='Video streaming dropout by category')
ax.set_xlabel("x label")
ax.set_ylabel("y label")

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

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

Alternatively, the index x-axis label is automatically set to the Index name, if it has one. so df2.index.name = 'x label' would work too.

这篇关于在 pandas 图上添加x和y标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆