Plotly Bubble chart from pandas crosstab

Iceberg_Slim

How can I plot a bubble chart from a dataframe that has been created from a pandas crosstab of another dataframe?

Imports;

import plotly as py
import plotly.graph_objects as go
from plotly.subplots import make_subplots

The crosstab was created using;

df = pd.crosstab(raw_data['Speed'], raw_data['Height'].fillna('n/a'))

The df contains mostly zeros, however where a number appears I want a point where the value controls the point size. I want to set the Index values as the x axis and the columns name values as the Y axis.

The df would look something like;

         10    20    30    40    50
1000     0     0    0      0     5
1100     0     0    0      7     0
1200     1     0    3      0     0
1300     0     0    0      0     0
1400     5     0    0      0     0

I’ve tried using scatter & Scatter like this;

fig.add_trace(go.Scatter(x=df.index.values, y=df.columns.values, size=df.values,
                         mode='lines'),
              row=1, col=3)

This returned a TypeError: 'Module' object not callable.

Any help is really appreciatted. Thanks

UPDATE

The answers below are close to what I ended up with, main difference being that I reference 'Speed' in the melt line;

df.reset_index()
df.melt(id_vars="Speed")
df.rename(columns={"index":"Engine Speed",
                    "variable":"Height",
                    "value":"Count"})
df[df!=0].dropna()

scale=1000

fig.add_trace(go.Scatter(x=df["Speed"], y=df["Height"],mode='markers',marker_size=df["Count"]/scale),
              row=1, col=3)

This works however my main problem now is that the dataset is huge and plotly is really struggling to deal with it.

Update 2

Using Scattergl allows Plotly to deal with the large dataset very well!

rpanai

If this is the case you can use plotly.express this is very similar to @Erik answer but shouldn't return errors.

import pandas as pd
import plotly.express as px
from io import StringIO

txt = """
        10    20    30    40    50
1000     0     0    0      0     5
1100     0     0    0      7     0
1200     1     0    3      0     0
1300     0     0    0      0     0
1400     5     0    0      0     0
"""

df = pd.read_csv(StringIO(txt), delim_whitespace=True)

df = df.reset_index()\
       .melt(id_vars="index")\
       .rename(columns={"index":"Speed",
                        "variable":"Height",
                        "value":"Count"})

fig = px.scatter(df, x="Speed", y="Height",size="Count")
fig.show()

enter image description here

UPDATE In case you got error please check your pandas version with pd.__version__ and try to check line by line this

df = pd.read_csv(StringIO(txt), delim_whitespace=True)

df = df.reset_index()

df = df.melt(id_vars="index")

df = df.rename(columns={"index":"Speed",
                        "variable":"Height",
                        "value":"Count"})

and report in which line it breaks.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Plot a bubble chart using plotly

plotly Line chart from pandas dataframe

Plotly grouped bar chart from pandas df

Trouble with Plotly line chart from Pandas

Label specific bubbles in Plotly bubble chart

size legend for plotly bubble map/chart

How to label bubble chart/scatter plot with column from pandas dataframe?

Plot bar chart with plotly from group_by() of Pandas

Annotated bubble chart from a dataframe

Plotly: How to plot multiple lines in one plotly chart from different columns from the same pandas dataframe?

Set text size within marker in r plotly bubble chart

Change legend marker size for Plotly scatter plot (bubble chart) in Python

Making bubble chart based on a pandas df

Pandas crosstab, but with values from aggregation of third column

Pandas: update a crosstab table from a dataframe

How to make a bubble chart from CSV file?

Plotly stacked bar chart pandas dataframe

Plot multiple lines into the same chart over time from pandas group by result using plotly

Delete text from th bars of plotly chart

Hide line from Plotly.js chart

Plotly R chart from two different dataframe

Python plotly logo disapering from chart

how to retrieve values from plotly sunburst chart?

Callback from sunburst chart. DASH PLOTLY

How do I create weighted scatter plot / bubble chart with color gradient using plotly R

Make a proper data frame from a pandas crosstab output

how to plot 100% bar chart from a stacked chart with plotly?

d3js: Hightlight bubble with specific parameters coming from input in bubble chart

Printing crosstab in python pandas

TOP Ranking

HotTag

Archive