在熊猫的故事中加入x和y标签。

时间:2021-10-03 20:26:19

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标签。

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.

我如何能够轻松地设置x和y标签,同时保留使用特定的colormaps的能力?我注意到熊猫DataFrames的plot()包装器不接受任何特定的参数。

5 个解决方案

#1


166  

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

函数的作用是:返回一个matplotlib.ax。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>

在熊猫的故事中加入x和y标签。

Or, more succinctly: ax.set(xlabel="x label", ylabel="y label").

或者,更简洁:ax。集(包含= " x标签”,ylabel = y标签)。

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.

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

#2


21  

If you label the columns and index of your DataFrame, pandas will automatically supply appropriate labels:

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

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')

在熊猫的故事中加入x和y标签。

In this case, you'll still need to supply y-labels manually (e.g., via plt.ylabel as shown in the other answers).

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

#3


20  

You can use do it like this:

你可以这样做:

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()

Obviously you have to replace the strings 'xlabel' and 'ylabel' with what you want them to be.

显然,你必须用你想要的东西来替换字符串'xlabel'和'ylabel'。

#4


11  

It is possible to set both labels together with axis.set function. Look for the example:

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

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()

在熊猫的故事中加入x和y标签。

#5


9  

For cases where you use pandas.DataFrame.hist:

如果你使用pandas.DataFrame.hist:

plt = df.Column_A.hist(bins=10)

Note that you get an ARRAY of plots, rather than a plot. Thus to set the x label you will need to do something like this

注意,你得到的是一系列的情节,而不是一个情节。因此,要设置x标签,您需要这样做。

plt[0][0].set_xlabel("column A")

#1


166  

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

函数的作用是:返回一个matplotlib.ax。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>

在熊猫的故事中加入x和y标签。

Or, more succinctly: ax.set(xlabel="x label", ylabel="y label").

或者,更简洁:ax。集(包含= " x标签”,ylabel = y标签)。

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.

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

#2


21  

If you label the columns and index of your DataFrame, pandas will automatically supply appropriate labels:

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

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')

在熊猫的故事中加入x和y标签。

In this case, you'll still need to supply y-labels manually (e.g., via plt.ylabel as shown in the other answers).

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

#3


20  

You can use do it like this:

你可以这样做:

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()

Obviously you have to replace the strings 'xlabel' and 'ylabel' with what you want them to be.

显然,你必须用你想要的东西来替换字符串'xlabel'和'ylabel'。

#4


11  

It is possible to set both labels together with axis.set function. Look for the example:

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

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()

在熊猫的故事中加入x和y标签。

#5


9  

For cases where you use pandas.DataFrame.hist:

如果你使用pandas.DataFrame.hist:

plt = df.Column_A.hist(bins=10)

Note that you get an ARRAY of plots, rather than a plot. Thus to set the x label you will need to do something like this

注意,你得到的是一系列的情节,而不是一个情节。因此,要设置x标签,您需要这样做。

plt[0][0].set_xlabel("column A")