Python 将 x 和 y 标签添加到熊猫图中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21487329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add x and y labels to a pandas plot
提问by Everaldo Aguiar
Suppose I have the following code that plots something very simple using 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')


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 标签,同时保留使用特定颜色图的能力?我注意到plot()pandas DataFrames的包装器没有采用任何特定于此的参数。
采纳答案by TomAugspurger
The df.plot()function returns a matplotlib.axes.AxesSubplotobject. You can set the labels on that object.
该df.plot()函数返回一个matplotlib.axes.AxesSubplot对象。您可以在该对象上设置标签。
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")


Or, more succinctly: ax.set(xlabel="x label", ylabel="y label").
或者,更简洁地说:ax.set(xlabel="x label", ylabel="y 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.
或者,索引 x 轴标签会自动设置为索引名称(如果有)。所以df2.index.name = 'x label'也会起作用。
回答by jesukumar
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' 替换为您想要的字符串。
回答by shoyer
If you label the columns and index of your DataFrame, pandas will automatically supply appropriate labels:
如果您标记 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')


In this case, you'll still need to supply y-labels manually (e.g., via plt.ylabelas shown in the other answers).
在这种情况下,您仍然需要手动提供 y 标签(例如,通过plt.ylabel其他答案中显示的方式)。
回答by Selah
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")
回答by Serenity
It is possible to set both labels together with axis.setfunction. Look for the example:
可以将两个标签与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()
回答by Dr. Arslan
pandasuses matplotlibfor basic dataframe plots. So, if you are using pandasfor basic plot you can use matplotlib for plot customization. However, I propose an alternative method here using seabornwhich allows more customization of the plot while not going into the basic level of matplotlib.
pandas使用matplotlib基本数据帧图。因此,如果您pandas用于基本绘图,则可以使用 matplotlib 进行绘图自定义。但是,我在这里提出了一种替代方法,seaborn它允许在不进入matplotlib.
Working Code:
工作代码:
import pandas as pd
import seaborn as sns
values = [[1, 2], [2, 5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'],
index=['Index 1', 'Index 2'])
ax= sns.lineplot(data=df2, markers= True)
ax.set(xlabel='xlabel', ylabel='ylabel', title='Video streaming dropout by category')
回答by Dror Hilman
what about ...
关于什么 ...
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()

