为使用 Pandas 绘制的绘图添加标签和标题 [英] Add labels and title to a plot made using pandas

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

问题描述

我使用以下代码制作了一个简单的直方图:

I made a simple histogram using the following code:

a = ['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'e', 'e', 'e', 'e', 'e']
pd.Series(a).value_counts().plot('bar')

虽然这是绘制频率直方图的简洁方法,但我不确定如何自定义绘图,即:

Although this is a concise way to plot frequency histogram, I am not sure how to customize the plot i.e. :

  1. 添加标题
  2. 添加轴标签
  3. 在 x 轴上对值进行排序

推荐答案

Series.plot(或 DataFrame.plot)返回一个 matplotlib axis> 公开多种方法的对象.例如:

Series.plot (or DataFrame.plot) returns a matplotlib axis object which exposes several methods. For example:

a = ['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'e', 'e', 'e', 'e', 'e']
ax = pd.Series(a).value_counts().sort_index().plot('bar')
ax.set_title("my title")
ax.set_xlabel("my x-label")
ax.set_ylabel("my y-label")

n.b.:pandas 在这里使用 matplotlib 作为依赖项,并公开 matplotlib 对象和 api.您可以通过 import matplotlib.pyplot as plt; 获得相同的结果;ax = plt.subplots(1,1,1).如果您一次创建多个图,您会发现 ax. 比模块级 plt.title('my title'),因为它定义了哪个你想改变的图标题,你可以利用 ax 对象上的自动完成功能.

n.b.: pandas uses matplotlib as a dependency here, and is exposing matplotlib objects and api. You can get the same result via import matplotlib.pyplot as plt; ax = plt.subplots(1,1,1). If you ever create more than one plot at a time, you will find the ax.<method> far more convenient than the module level plt.title('my title'), because it defines which plot title you'd like to change and you can take advantage of autocomplete on the ax object.

这篇关于为使用 Pandas 绘制的绘图添加标签和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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