首页 > 解决方案 > 在 python 上使用 swarmplot 绘制和建模文本数据

问题描述

我有一个 csv 文件,我想使用 seaborn 库的 swarmplot 来绘制两个选定列之间的关系。

这是我正在使用的 csv 文件中的 5 行示例

    SeriesCode       Year                 DESCRIPTION
21  IC.FRM.CORR.ZS   YR2004 The sample was drawn from the manufacturing sector only.
38  SP.ADO.TFRT      YR2010 Interpolated using data for 2007 and 2012.  
10  SP.ADO.TFRT      YR2000 Interpolated using data for 1997 and 2002.  
18  IC.FRM.CORR.ZS   YR2003 The sample was drawn from the manufacturing sector only.
32  IC.TAX.METG      YR2007 The sample was drawn from the manufacturing sector only.    
28  SP.ADO.TFRT      YR2006 Interpolated using data for 2002 and 2007.  

我有这段代码

import re
import pandas


df1=pandas.read_csv("./Jobs_csv/JobsSeries-Time.csv")
ifcz=df1[df1['SeriesCode'].str.contains("IC.FRM.CORR.ZS",flags=re.IGNORECASE,regex=True)].DESCRIPTION
ify=df1[df1['SeriesCode'].str.contains("IC.FRM.CORR.ZS",flags=re.IGNORECASE,regex=True)].Year
sb.swarmplot(x="ifcz", y="ify", data=df1)

但每当我运行它

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-68-3c5933ceba52> in <module>()
----> 1 sb.swarmplot(x="ifcz", y="ify", data=df1)

/home/user/.local/lib/python3.6/site-packages/seaborn/categorical.py in swarmplot(x, y, hue, data, order, hue_order, dodge, orient, color, palette, size, edgecolor, linewidth, ax, **kwargs)
   2975 
   2976     plotter = _SwarmPlotter(x, y, hue, data, order, hue_order,
-> 2977                             dodge, orient, color, palette)
   2978     if ax is None:
   2979         ax = plt.gca()

/home/user/.local/lib/python3.6/site-packages/seaborn/categorical.py in __init__(self, x, y, hue, data, order, hue_order, dodge, orient, color, palette)
   1214                  dodge, orient, color, palette):
   1215         """Initialize the plotter."""
-> 1216         self.establish_variables(x, y, hue, data, orient, order, hue_order)
   1217         self.establish_colors(color, palette, 1)
   1218 

/home/user/.local/lib/python3.6/site-packages/seaborn/categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
    150                 if isinstance(var, str):
    151                     err = "Could not interpret input '{}'".format(var)
--> 152                     raise ValueError(err)
    153 
    154             # Figure out the plotting orientation

ValueError: Could not interpret input 'ifcz'

我收到这些错误。我不知道它为什么会出现此错误或如何修复它。而且我不确定是否应该使用 swarmplot。如果你认为这是因为不应该使用 swarmplot,那么你能说出其他的吗情节来建模这个?

标签: python-3.xpandascsvmatplotlibseaborn

解决方案


推荐阅读