单变量类别散点图 pandas
Single variable category scatter plot pandas
是否可以将单个值绘制为散点图?
我可以很好地通过获取带有标记的 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
我可以用 bokeh
和 pandas
使用 x
和 y
值编写散点图代码。但是在单值的情况下 ?
当所有输入合并为一个输入并按 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)
样本输出
你可以试试 boxplot or violinplot。或者,如果您不喜欢这些并且只想要点的垂直分布,您可以强制散点图沿着单个 x 值绘制。为此,您需要创建一个固定值数组(例如 1),该数组的长度与您要绘制的数组的长度相同:
ones = []
for range(len(data)):
ones.append(1)
plt.scatter(ones,data)
plt.show()
那会给你这样的东西:
更新:
查看 Bokeh and 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)
您可以在 x 轴上绘制索引,在 y 轴上绘制列值
df = pd.DataFrame(np.random.randint(0,10,size=(100, 1)), columns=list('A'))
sns.scatterplot(data=df['A'])
我经常使用的是 "size plot" – 一种与您请求的可视化类似的可视化,其中可以跨组比较单个特征。 这是一个使用您的数据的示例:
下面是实现这个尺寸图的代码:
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()
是否可以将单个值绘制为散点图? 我可以很好地通过获取带有标记的 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
我可以用 bokeh
和 pandas
使用 x
和 y
值编写散点图代码。但是在单值的情况下 ?
当所有输入合并为一个输入并按 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)
样本输出
你可以试试 boxplot or violinplot。或者,如果您不喜欢这些并且只想要点的垂直分布,您可以强制散点图沿着单个 x 值绘制。为此,您需要创建一个固定值数组(例如 1),该数组的长度与您要绘制的数组的长度相同:
ones = []
for range(len(data)):
ones.append(1)
plt.scatter(ones,data)
plt.show()
那会给你这样的东西:
更新:
查看 Bokeh and 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)
您可以在 x 轴上绘制索引,在 y 轴上绘制列值
df = pd.DataFrame(np.random.randint(0,10,size=(100, 1)), columns=list('A'))
sns.scatterplot(data=df['A'])
我经常使用的是 "size plot" – 一种与您请求的可视化类似的可视化,其中可以跨组比较单个特征。 这是一个使用您的数据的示例:
下面是实现这个尺寸图的代码:
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()