我正在尝试绘制一个seaborn swarmplot,其中col [2]是freq,col [3]是要分组的类。下面给出了输入和代码。 输入
tweetcricscore,51,high active
tweetcricscore,46,event based
tweetcricscore,12,event based
tweetcricscore,46,event based
tweetcricscore,1,viewers
tweetcricscore,178,viewers
tweetcricscore,46,situational
tweetcricscore,23,situational
tweetcricscore,1,situational
tweetcricscore,8,situational
tweetcricscore,56,situational
代码:
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="whitegrid", color_codes=True)
df = pd.read_csv('input.csv', header = None)
df.columns = ['keyword','freq','class']
ax = sns.swarmplot(x="class", y="freq", data=df)
plt.show()
代码不会绘制,也不会产生任何错误。有什么建议来优化代码?
答案 0 :(得分:1)
我认为您首先需要read_csv
,然后通过与fillna
和最后strip
个空格的concanecate创建新的列类:
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import io
temp=u"""tweetcricscore 51 high active
tweetcricscore 46 event based
tweetcricscore 12 event based
tweetcricscore 46 event based
tweetcricscore 1 viewers
tweetcricscore 178 viewers
tweetcricscore 46 situational
tweetcricscore 23 situational
tweetcricscore 1 situational
tweetcricscore 8 situational
tweetcricscore 56 situational"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp),
sep="\s+", #separator is arbitrary whitespace
names=['keyword','freq','class1','class2']) #set new col names
df['class'] = df['class1'] + ' ' + df['class2'].fillna('')
df['class'] = df['class'].str.strip()
print df
keyword freq class1 class2 class
0 tweetcricscore 51 high active high active
1 tweetcricscore 46 event based event based
2 tweetcricscore 12 event based event based
3 tweetcricscore 46 event based event based
4 tweetcricscore 1 viewers NaN viewers
5 tweetcricscore 178 viewers NaN viewers
6 tweetcricscore 46 situational NaN situational
7 tweetcricscore 23 situational NaN situational
8 tweetcricscore 1 situational NaN situational
9 tweetcricscore 8 situational NaN situational
10 tweetcricscore 56 situational NaN situational
sns.set(style="whitegrid", color_codes=True)
ax = sns.swarmplot(x="class", y="freq", data=df)
plt.show()
如果列class
不包含空格,请解决此问题:
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import io
temp=u"""tweetcricscore 51 highactive
tweetcricscore 46 eventbased
tweetcricscore 12 eventbased
tweetcricscore 46 eventbased
tweetcricscore 1 viewers
tweetcricscore 178 viewers
tweetcricscore 46 situational
tweetcricscore 23 situational
tweetcricscore 1 situational
tweetcricscore 8 situational
tweetcricscore 56 situational"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp),
sep="\s+", #separator is arbitrary whitespace
names=['keyword','freq','class']) #set new col names
print df
keyword freq class
0 tweetcricscore 51 highactive
1 tweetcricscore 46 eventbased
2 tweetcricscore 12 eventbased
3 tweetcricscore 46 eventbased
4 tweetcricscore 1 viewers
5 tweetcricscore 178 viewers
6 tweetcricscore 46 situational
7 tweetcricscore 23 situational
8 tweetcricscore 1 situational
9 tweetcricscore 8 situational
10 tweetcricscore 56 situational
sns.set(style="whitegrid", color_codes=True)
ax = sns.swarmplot(x="class", y="freq", data=df)
plt.show()
EDIT2:
如果分隔符为,
,请使用:
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import io
temp=u"""tweetcricscore,51,high active
tweetcricscore,46,event based
tweetcricscore,12,event based
tweetcricscore,46,event based
tweetcricscore,1,viewers
tweetcricscore,178,viewers
tweetcricscore,46,situational
tweetcricscore,23,situational
tweetcricscore,1,situational
tweetcricscore,8,situational
tweetcricscore,56,situational"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), names=['keyword','freq','class'])
print df
keyword freq class
0 tweetcricscore 51 high active
1 tweetcricscore 46 event based
2 tweetcricscore 12 event based
3 tweetcricscore 46 event based
4 tweetcricscore 1 viewers
5 tweetcricscore 178 viewers
6 tweetcricscore 46 situational
7 tweetcricscore 23 situational
8 tweetcricscore 1 situational
9 tweetcricscore 8 situational
10 tweetcricscore 56 situational
sns.set(style="whitegrid", color_codes=True)
ax = sns.swarmplot(x="class", y="freq", data=df)
plt.show()
答案 1 :(得分:0)
在使用swamplot
行以及jezreal的不断帮助和建议的数据集绘制8-10k
之后的几条路径之后。我们得出结论,seaborn
类别绘图swarmplot
无法像seaborn
中的其他绘图一样扩展大数据,这也是教程文档中提到的。因此,我将绘图样式更改为bokeh
散点图,其中我使用y
轴上的数值和x
轴上的分组类别名称,这有点解决了我绘制{{1}的问题用类别绘制数据。
univariate
这允许按import numpy as np
import matplotlib.pyplot as plt
from pylab import*
import math
from matplotlib.ticker import LogLocator
import pandas as pd
from bokeh.models import BoxSelectTool, BoxZoomTool, LassoSelectTool
from bokeh.charts import Scatter, output_file, show
from bokeh.plotting import figure, hplot, vplot
from bokeh.models import LinearAxis
df = pd.read_csv('input.csv', header = None)
df.columns = ['user','freq','class']
scatter = Scatter( df, x='class', y='freq', color='class', marker='class', title=' User classification', legend=False)
output_file('output.html', title='output')
show(scatter)
列分组,根据组分配颜色和标记。沿y轴绘制class
。
注意:这可能意外地起作用,因为数据是离散的是自然。