Home > other >  Plotting 3D plot with cross-correlation and different marker sizes to show N
Plotting 3D plot with cross-correlation and different marker sizes to show N

Time:10-08

I have a dataframe like this:

enter image description here

data = [[1, 2, 3], [2, 2, 3], [1, 1, 2], [2, 2, 2], [2, 3, 2], [2, 3, 3], [1, 1, 1], [3, 3, 3], [3, 3, 2], [1, 2, 1], [1, 3, 1], [3, 1, 3], [2, 1, 2], [3, 3, 3], [2, 2, 2], [3, 2, 1], [3, 2, 2], [2, 2, 1], [1, 1, 3], [1, 3, 2], [1, 2, 3]]
df = pd.DataFrame(data, columns=['Math', 'Science', 'English'])

Where each row depicts a student. The ratings 1-3 are as follows: 1 is Poor grade, 2 is Average grade, 3 is Good grade

What I'm interested in doing is to create a 3D plot that shows correlation between a student's grades and also shows N (number).

I'm not sure if the subjects should be on the x y z axis, or their grade (would like to try both). Instead of just a 3D scatter plot, I'll like markers in the same place (for example, a student who has 3 (good grade) in all 3 categories) to be bigger to show the N. Essentially, I'll like the size of the marker to show the N, and either their grade or subject (or both) to be colour-coded.

Is there a way to visualize this type of data in a clear manner? I was thinking of something like a 3D venn diagram, but I cannot figure it out. I tried using multi-hierarchy circlify but couldn't achieve a 3D aspect, where it cross-correlates both grades and subjects. Any guidance will be super appreciated!

edit: N values for this sample dataframe:

enter image description here enter image description here

CodePudding user response:

I created some sample data with the same format as yours with 200 students having ratings of 1-3 occuring pseudo-randomly (but with a higher proportion of 2's and 3's so that we can see a pattern).

Additional edits: (1) you can use a mapping to replace the ratings 1,2,3 with "poor", "average","good" in the df itself, and plotly will understand this is meant to be categorical data and reflect this in the axes on the 3d scatter and (2) to color the markers in a consistent way, we need to combine the ratings from the subjects – the most straightforward way is to create a new column "sum" that is the sum of ratings from all three subjects, and pass the name of this column to px.scatter_3d

import numpy as np
import pandas as pd
import plotly.express as px

## create some random data where there will be clusters
np.random.seed(42)
data = np.random.choice([1,2,3],size=[200,3], p=[0.2,0.3,0.5])
df = pd.DataFrame(data, columns=['Math', 'Science', 'English'])

rating_map = {1: 'poor', 2: 'average', 3:'good'}

## count the number of times each unique combination of grades occurs
df_counts = df.value_counts().rename('counts').reset_index()
df_counts['sum'] = df_counts['Math']   df_counts['Science']   df_counts['English']
df_counts[['Math','Science','English']] = df_counts[['Math','Science','English']].applymap(lambda x: rating_map[x])

fig = px.scatter_3d(df_counts, x='Math', y='Science', z='English', size='counts', size_max=50, color='sum')

fig.show()

enter image description here

CodePudding user response:

Check out Derek's Visualization live, use "Run code snippet" and then hit "Full page"~

Produced by doing:

print(fig.to_html(default_width=600, 
                  default_height=600, 
                  include_plotlyjs='cdn'))

<html>
<head><meta charset="utf-8" /></head>
<body>
    <div>                        <script type="text/javascript">window.PlotlyConfig = {MathJaxConfig: 'local'};</script>
        <script src="https://cdn.plot.ly/plotly-2.12.1.min.js"></script>                <div id="a6e33e0b-a196-46ad-87c5-a6c903f4cfcd"  style="height:600px; width:600px;"></div>            <script type="text/javascript">                                    window.PLOTLYENV=window.PLOTLYENV || {};                                    if (document.getElementById("a6e33e0b-a196-46ad-87c5-a6c903f4cfcd")) {                    Plotly.newPlot(                        "a6e33e0b-a196-46ad-87c5-a6c903f4cfcd",                        [{"hovertemplate":"Math=%{x}<br>Science=%{y}<br>English=%{z}<br>counts=%{marker.size}<extra></extra>","legendgroup":"","marker":{"color":"#636efa","size":[32,17,12,11,10,10,10,9,8,8,8,7,7,7,6,5,5,4,4,4,4,3,3,2,2,1,1],"sizemode":"area","sizeref":0.0128,"symbol":"circle"},"mode":"markers","name":"","scene":"scene","showlegend":false,"x":[3,3,1,3,3,3,2,3,2,2,2,3,2,3,1,1,1,3,1,1,1,2,2,2,2,1,1],"y":[3,3,3,3,2,2,3,1,1,2,3,1,3,2,3,2,2,1,1,3,1,1,1,2,2,1,2],"z":[3,2,3,1,3,1,3,3,3,3,1,1,2,2,2,3,2,2,1,1,3,2,1,1,2,2,1],"type":"scatter3d"}],                        {"template":{"data":{"histogram2dcontour":[{"type":"histogram2dcontour","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"choropleth":[{"type":"choropleth","colorbar":{"outlinewidth":0,"ticks":""}}],"histogram2d":[{"type":"histogram2d","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"heatmap":[{"type":"heatmap","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"heatmapgl":[{"type":"heatmapgl","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"contourcarpet":[{"type":"contourcarpet","colorbar":{"outlinewidth":0,"ticks":""}}],"contour":[{"type":"contour","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"surface":[{"type":"surface","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"mesh3d":[{"type":"mesh3d","colorbar":{"outlinewidth":0,"ticks":""}}],"scatter":[{"fillpattern":{"fillmode":"overlay","size":10,"solidity":0.2},"type":"scatter"}],"parcoords":[{"type":"parcoords","line":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterpolargl":[{"type":"scatterpolargl","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"bar":[{"error_x":{"color":"#2a3f5f"},"error_y":{"color":"#2a3f5f"},"marker":{"line":{"color":"#E5ECF6","width":0.5},"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"bar"}],"scattergeo":[{"type":"scattergeo","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterpolar":[{"type":"scatterpolar","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"histogram":[{"marker":{"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"histogram"}],"scattergl":[{"type":"scattergl","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatter3d":[{"type":"scatter3d","line":{"colorbar":{"outlinewidth":0,"ticks":""}},"marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scattermapbox":[{"type":"scattermapbox","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterternary":[{"type":"scatterternary","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scattercarpet":[{"type":"scattercarpet","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"carpet":[{"aaxis":{"endlinecolor":"#2a3f5f","gridcolor":"white","linecolor":"white","minorgridcolor":"white","startlinecolor":"#2a3f5f"},"baxis":{"endlinecolor":"#2a3f5f","gridcolor":"white","linecolor":"white","minorgridcolor":"white","startlinecolor":"#2a3f5f"},"type":"carpet"}],"table":[{"cells":{"fill":{"color":"#EBF0F8"},"line":{"color":"white"}},"header":{"fill":{"color":"#C8D4E3"},"line":{"color":"white"}},"type":"table"}],"barpolar":[{"marker":{"line":{"color":"#E5ECF6","width":0.5},"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"barpolar"}],"pie":[{"automargin":true,"type":"pie"}]},"layout":{"autotypenumbers":"strict","colorway":["#636efa","#EF553B","#00cc96","#ab63fa","#FFA15A","#19d3f3","#FF6692","#B6E880","#FF97FF","#FECB52"],"font":{"color":"#2a3f5f"},"hovermode":"closest","hoverlabel":{"align":"left"},"paper_bgcolor":"white","plot_bgcolor":"#E5ECF6","polar":{"bgcolor":"#E5ECF6","angularaxis":{"gridcolor":"white","linecolor":"white","ticks":""},"radialaxis":{"gridcolor":"white","linecolor":"white","ticks":""}},"ternary":{"bgcolor":"#E5ECF6","aaxis":{"gridcolor":"white","linecolor":"white","ticks":""},"baxis":{"gridcolor":"white","linecolor":"white","ticks":""},"caxis":{"gridcolor":"white","linecolor":"white","ticks":""}},"coloraxis":{"colorbar":{"outlinewidth":0,"ticks":""}},"colorscale":{"sequential":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]],"sequentialminus":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]],"diverging":[[0,"#8e0152"],[0.1,"#c51b7d"],[0.2,"#de77ae"],[0.3,"#f1b6da"],[0.4,"#fde0ef"],[0.5,"#f7f7f7"],[0.6,"#e6f5d0"],[0.7,"#b8e186"],[0.8,"#7fbc41"],[0.9,"#4d9221"],[1,"#276419"]]},"xaxis":{"gridcolor":"white","linecolor":"white","ticks":"","title":{"standoff":15},"zerolinecolor":"white","automargin":true,"zerolinewidth":2},"yaxis":{"gridcolor":"white","linecolor":"white","ticks":"","title":{"standoff":15},"zerolinecolor":"white","automargin":true,"zerolinewidth":2},"scene":{"xaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2},"yaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2},"zaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2}},"shapedefaults":{"line":{"color":"#2a3f5f"}},"annotationdefaults":{"arrowcolor":"#2a3f5f","arrowhead":0,"arrowwidth":1},"geo":{"bgcolor":"white","landcolor":"#E5ECF6","subunitcolor":"white","showland":true,"showlakes":true,"lakecolor":"white"},"title":{"x":0.05},"mapbox":{"style":"light"}}},"scene":{"domain":{"x":[0.0,1.0],"y":[0.0,1.0]},"xaxis":{"title":{"text":"Math"}},"yaxis":{"title":{"text":"Science"}},"zaxis":{"title":{"text":"English"}}},"legend":{"tracegroupgap":0,"itemsizing":"constant"},"margin":{"t":60}},                        {"responsive": true}                    )                };                            </script>        </div>
</body>
</html>

  • Related