将x和y标签添加到pandas图中

Eve*_*iar 153 python matplotlib dataframe pandas

假设我有以下代码使用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')
Run Code Online (Sandbox Code Playgroud)

产量

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

Tom*_*ger 271

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

In [4]: ax = df2.plot(lw=2, colormap='jet', marker='.', markersize=10, title='Video streaming dropout by category')

In [6]: ax.set_xlabel("x label")
Out[6]: <matplotlib.text.Text at 0x10e0af2d0>

In [7]: ax.set_ylabel("y label")
Out[7]: <matplotlib.text.Text at 0x10e0ba1d0>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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

  • 是否有一个特殊的原因,为什么x和y标签不能作为参数添加到`pd.plot()`?鉴于`pd.plot()`对`plt.plot()`的额外简洁,似乎使它更简洁而不必调用`ax.set_ylabel()`是有意义的. (62认同)

jes*_*mar 33

你可以这样做:

import matplotlib.pyplot as plt 
import pandas as pd

plt.figure()
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')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
plt.show()
Run Code Online (Sandbox Code Playgroud)

显然你必须用你想要的字符串替换字符串'xlabel'和'ylabel'.


sho*_*yer 28

如果您标记DataFrame的列和索引,pandas将自动提供适当的标签:

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

在此输入图像描述

在这种情况下,您仍然需要手动提供y标签(例如,通过plt.ylabel其他答案中显示).


Ser*_*ity 17

可以将两个标签与axis.set功能一起设置.寻找例子:

import pandas as pd
import matplotlib.pyplot as plt
values = [[1,2], [2,5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'], index=['Index 1','Index 2'])
ax = df2.plot(lw=2,colormap='jet',marker='.',markersize=10,title='Video streaming dropout by category')
# set labels for both axes
ax.set(xlabel='x axis', ylabel='y axis')
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 我喜欢`.set(xlabel ='x axis',ylabel ='y axis')`解决方案,因为它让我把它全部放在一行中,这与set_xlabel和set_ylabel绘图方法不同.我想知道为什么他们所有(包括set方法,顺便说一句)不返回plot对象或至少从它继承的东西. (3认同)

Sel*_*lah 13

对于您使用的情况pandas.DataFrame.hist:

plt = df.Column_A.hist(bins=10)
Run Code Online (Sandbox Code Playgroud)

请注意,您获得的是绘图的ARRAY,而不是绘图.因此,要设置x标签,您需要执行类似的操作

plt[0][0].set_xlabel("column A")
Run Code Online (Sandbox Code Playgroud)


Dro*_*man 12

关于什么 ...

import pandas as pd
import matplotlib.pyplot as plt

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')
    .set(xlabel='x axis',
         ylabel='y axis'))

plt.show()
Run Code Online (Sandbox Code Playgroud)


Myk*_*tko 6

在 Pandas 1.10 版中,您可以使用参数xlabelylabel方法plot

df.plot(xlabel='X Label', ylabel='Y Label', title='Plot Title')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明