【问题标题】:Single variable category scatter plot pandas单变量类别散点图 pandas
【发布时间】:2020-03-16 12:56:49
【问题描述】:

是否可以将单个值绘制为散点图? 我可以通过获取带有标记的 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

我可以使用x 和y 值编写带有bokeh 和pandas 的散点图代码。但是在单值的情况下呢?

当所有输入合并为一个输入并按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)

样本输出

【问题讨论】:

  • 对于 scetterplot 你必须说你想要什么作为 x 轴和 y 轴。或者,您可能有一个 barplot 具有类别/等。名称为 x 轴。
  • 是的,很明显需要散点图 x 和 y。但在这种情况下,我将单变量数据作为不同类别的输出。线和条很常见。我正在努力寻找更好的可视化类型。
  • @MaxU 不绘制条形图的另一个原因是输入范围为 1 到 1000 个值
  • 您将拥有多少个不同的类别?我不是询问价值观...
  • 可能是swarmplot?

标签: python pandas matplotlib seaborn bokeh


【解决方案1】:

更新:

查看Bokeh 和Seaborn 画廊 - 它可能会帮助您了解哪种情节适合您的需求

你可以试试这样的 violinplot:

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

或热图:

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】:

您可以尝试boxplot 或violinplot。或者,如果您不喜欢这些并且只想要点的垂直分布,您可以强制散点图沿单个 x 值绘制。为此,您需要创建一个与您将要绘制的数组长度相同的固定值(例如 1)数组:

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

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

这会给你这样的东西:

【讨论】:

  • 感谢 Grr 的解决方案。这不是不喜欢线或条形图。只是它们太常见了,想要搜索是否有更多有趣的可视化可用,我应该知道。
  • 不绘制条形图的另一个原因是输入范围为 1 到 1000 个值
  • 群图怎么样?
  • 是的,这很好。文档是here。 This question 有一个很好的 swarmplot boxplot 组合示例。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    我经常使用的东西是“尺寸图”——一种类似于您要求的可视化,其中可以跨组比较单个特征。 以下是使用您的数据的示例:

    这是实现这个尺寸图的代码:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多