首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将x和y标签添加到熊猫图中

将x和y标签添加到熊猫图中
EN

Stack Overflow用户
提问于 2014-02-01 02:23:10
回答 6查看 402.7K关注 0票数 245

假设我有以下代码,它使用pandas绘制一些非常简单的东西:

代码语言:javascript
复制
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()包装器没有接受任何特定的参数。

EN

回答 6

Stack Overflow用户

发布于 2014-02-01 02:35:35

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

代码语言:javascript
复制
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'也可以工作。

票数 401
EN

Stack Overflow用户

发布于 2014-02-01 02:52:56

您可以像这样使用do it:

代码语言:javascript
复制
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()

显然,您必须将字符串'xlabel‘和'ylabel’替换为您想要的值。

票数 54
EN

Stack Overflow用户

发布于 2014-12-17 16:06:02

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

代码语言:javascript
复制
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')

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

票数 32
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21487329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档