在matplotlib中将字符串打印为轴

2024-09-28 16:58:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pandas数据帧,类似于:

Species  Count1 Count2 
AGREP      2       10
GYLEP      4       6
POAPRA     2       8
EUPESU     1       11

我想画一个物种与计数1的直线图,x轴是物种,y轴是计数1。在

我试着使用代码:

^{pr2}$

但这将返回错误:

ValueError: could not convert string to float

我只想最终只选择某些物种来对付这两种情况。例如,我想在x轴上绘制AGREPEUPESUS,将计数1和计数2作为y轴。不过,它不会将字符串打印为轴名。在


Tags: 数据代码pandas物种错误直线计数species
1条回答
网友
1楼 · 发布于 2024-09-28 16:58:30

您可以将这些字符串设置为索引,只需使用pandas.DataFrame.plot方法。在

import pandas as pd

# your data
# ===================================

df


  Species  Count1  Count2
0   AGREP       2      10
1   GYLEP       4       6
2  POAPRA       2       8
3  EUPESU       1      11


# plot
# ===================================
df.set_index('Species').plot()

enter image description here

^{pr2}$

enter image description here

相关问题 更多 >