假设我有以下代码,它使用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标签,同时保留使用特定色彩映射表的能力?我注意到pandas DataFrames的plot()
包装器没有接受任何特定的参数。
发布于 2014-02-01 02:35:35
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")
或者,更简洁地说:ax.set(xlabel="x label", ylabel="y label")
。
或者,索引x轴标签会自动设置为索引名称(如果有)。所以df2.index.name = 'x label'
也可以工作。
发布于 2014-02-01 02:52:56
您可以像这样使用do it:
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’替换为您想要的值。
发布于 2014-12-17 16:06:02
如果您标记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')
在这种情况下,您仍然需要手动提供y标签(例如,通过其他答案中所示的plt.ylabel
)。
https://stackoverflow.com/questions/21487329
复制相似问题