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'也会工作.
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)
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)
在 Pandas 1.10 版中,您可以使用参数xlabel和ylabel方法plot:
df.plot(xlabel='X Label', ylabel='Y Label', title='Plot Title')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
223228 次 |
| 最近记录: |