单变量类散点图熊猫

时间:2016-05-12 18:38:17

标签: python pandas matplotlib seaborn bokeh

是否可以将单个值绘制为散点图? 我可以通过使用标记获取ccdfs来很好地绘制它,但我想知道是否有任何替代方案可用?

输入:

输入1

tweetcricscore 51 high active

输入2

tweetcricscore 46 event based
tweetcricscore 12 event based
tweetcricscore 46 event based

输入3

tweetcricscore 1 viewers 
tweetcricscore 178 viewers

输入4

tweetcricscore 46 situational
tweetcricscore 23 situational
tweetcricscore 1 situational
tweetcricscore 8 situational
tweetcricscore 56 situational

我可以使用bokehpandas值来编写包含xy的散点图代码。但是在单值的情况下?

当所有输入合并为一个输入并按col[3]分组时,值为col[2]

以下代码适用于包含2个变量

的数据集
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.charts import Scatter, output_file, show

df = pd.read_csv('input.csv', header = None)

df.columns = ['col1','col2','col3','col4']

scatter = Scatter( df, x='col2', y='col3', color='col4', marker='col4', title='plot', legend=True)

output_file('output.html', title='output')

show(scatter)

示例输出

enter image description here

4 个答案:

答案 0 :(得分:1)

您可以尝试boxplotviolinplot。或者,如果您不喜欢这些并且只想要垂直分布点,则可以强制散射沿单个x值绘制。要做到这一点,你需要创建一个固定值(比如说1)的数组,它与你要绘制的数组的长度相同:

ones = []
for range(len(data)):
    ones.append(1)

plt.scatter(ones,data)
plt.show()

那会给你这样的东西:

enter image description here

答案 1 :(得分:1)

<强>更新

查看BokehSeaborn画廊 - 它可能有助于您了解哪种情节符合您的需求

你可以尝试这样的小提琴情节:

sns.violinplot(x="category", y="val", data=df)

enter image description here

或HeatMaps:

import numpy as np
import pandas as pd
from bokeh.charts import HeatMap, output_file, show

cats = ['active', 'based', 'viewers', 'situational']
df = pd.DataFrame({'val': np.random.randint(1,100, 1000), 'category': np.random.choice(cats, 1000)})

hm = HeatMap(df)
output_file('d:/temp/heatmap.html')
show(hm)

答案 2 :(得分:0)

您可以在x轴上绘制索引,在y轴上绘制列值

df = pd.DataFrame(np.random.randint(0,10,size=(100, 1)), columns=list('A'))
sns.scatterplot(data=df['A'])

enter image description here

答案 3 :(得分:0)

我经常使用的是“尺寸图” –一种类似于您所要求的可视化效果,可以在各个组之间比较单个功能。 以下是使用您的数据的示例:

a size plot made using matplotlib

以下是实现此尺寸图的代码:

fig, ax = plt.subplots(1,1, figsize=(8,5))

colors = ['blue','green','orange','pink']

yticks = {"ticks":[],"labels":[]}
xticks = {"ticks":[],"labels":[]}

agg_functions = ["mean","std","sum"]

# Set size plot
for i, (label, group_df) in enumerate(df.groupby('type', as_index=False)):

    # Set tick
    yticks["ticks"].append(i)
    yticks["labels"].append(label)

    agg_values = group_df["tweetcricscore"].aggregate(agg_functions)

    for ii, (agg_f, x) in enumerate(agg_values.iteritems()):
        ax.scatter(x=ii, y = i, label=agg_f, s=x, color=colors[i])


        # Add your x axis
        if ii not in xticks["ticks"]:
            xticks["ticks"].append(ii)
            xticks["labels"].append(agg_f)


# Set yticks:
ax.set_yticks(yticks["ticks"]) 
ax.set_yticklabels(yticks["labels"], fontsize=12)

ax.set_xticks(xticks["ticks"]) 
ax.set_xticklabels(xticks["labels"], fontsize=12)


plt.show()
相关问题