Sitz Blogz
Sitz Blogz

Reputation: 1061

Single variable category scatter plot pandas

Is It possible to plot single value as scatter plot? I can very well plot it in line by getting the ccdfs with markers but I want to know if any alternative is available?

Input:

Input 1

tweetcricscore 51 high active

Input 2

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

Input 3

tweetcricscore 1 viewers 
tweetcricscore 178 viewers

Input 4

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

I can very much write scatter plot code with bokeh and pandas using x and y values. But in case of single value ?

When all the inputs are merged as one input and are to be grouped by col[3], values are col[2].

The code below is for data set with 2 variables

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)

Sample Output

enter image description here

Upvotes: 1

Views: 25583

Answers (4)

Yaakov Bressler
Yaakov Bressler

Reputation: 12168

Something I use rather regularly is a "size plot" – a visualization similar to the one you're requesting where a single feature can be compared across groups. Here is an example using your data:

a size plot made using matplotlib

Here is the code to achieve this 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()

Upvotes: 0

H_J
H_J

Reputation: 486

You can plot index on x-axis and column value on y-axis

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

enter image description here

Upvotes: 0

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

UPDATE:

look at Bokeh and Seaborn galleries - it might help you to understand what kind of plot fits your needs

you may try violinplot like this:

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

enter image description here

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

Upvotes: 1

Grr
Grr

Reputation: 16109

You could try a boxplot or violinplot. Alternatively if you don't like these and just want a vertical distribution of dots you could force a scatter to plot along a single x value. To do this you would need to create an array of a fixed value (say 1) that is the same length as the array you will be plotting:

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

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

That will give you something like this:

enter image description here

Upvotes: 2

Related Questions