Skip to content

Instantly share code, notes, and snippets.

@hopezh
Created January 27, 2022 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopezh/df0dadb15ddc80821bd051f2094da159 to your computer and use it in GitHub Desktop.
Save hopezh/df0dadb15ddc80821bd051f2094da159 to your computer and use it in GitHub Desktop.
ploty 3d chart, x axis range and nticks not shown as specified
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np \n",
"import pandas as pd\n",
"import plotly.express as px\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1. , -0.8, 0. ],\n",
" [ 0. , 1. , 0. ],\n",
" [ 1. , 0. , 0. ]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create a numpy array of the xyz coordinates of 3 pts\n",
"A = np.array(\n",
" [[1, -0.8, 0], \n",
" [0, 1, 0], \n",
" [1, 0, 0]]\n",
")\n",
"\n",
"A\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>x</th>\n",
" <th>y</th>\n",
" <th>z</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.0</td>\n",
" <td>-0.8</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" x y z\n",
"0 1.0 -0.8 0.0\n",
"1 0.0 1.0 0.0\n",
"2 1.0 0.0 0.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# turn numpy array into pandas dataframe\n",
"A_df = pd.DataFrame(A, columns=['x', 'y', 'z'])\n",
"A_df\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"hovertemplate": "x=%{x}<br>y=%{y}<br>z=%{z}<extra></extra>",
"legendgroup": "",
"marker": {
"color": "#636efa",
"size": 5,
"symbol": "circle"
},
"mode": "markers",
"name": "",
"scene": "scene",
"showlegend": false,
"type": "scatter3d",
"x": [
1,
0,
1
],
"y": [
-0.8,
1,
0
],
"z": [
0,
0,
0
]
}
],
"layout": {
"height": 800,
"legend": {
"tracegroupgap": 0
},
"margin": {
"t": 60
},
"scene": {
"aspectmode": "cube",
"aspectratio": {
"x": 1,
"y": 1,
"z": 1
},
"camera": {
"projection": {
"type": "orthographic"
}
},
"domain": {
"x": [
0,
1
],
"y": [
0,
1
]
},
"xaxis": {
"autorange": "reversed",
"nticks": 5,
"range": [
0,
2
],
"tickcolor": "yellow",
"ticks": "outside",
"tickwidth": 5,
"title": {
"text": "x"
}
},
"yaxis": {
"autorange": "reversed",
"nticks": 5,
"range": [
-1,
1
],
"title": {
"text": "y"
}
},
"zaxis": {
"nticks": 5,
"range": [
-1,
1
],
"title": {
"text": "z"
}
}
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#f2f5fa"
},
"error_y": {
"color": "#f2f5fa"
},
"marker": {
"line": {
"color": "rgb(17,17,17)",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "rgb(17,17,17)",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#A2B1C6",
"gridcolor": "#506784",
"linecolor": "#506784",
"minorgridcolor": "#506784",
"startlinecolor": "#A2B1C6"
},
"baxis": {
"endlinecolor": "#A2B1C6",
"gridcolor": "#506784",
"linecolor": "#506784",
"minorgridcolor": "#506784",
"startlinecolor": "#A2B1C6"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
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,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
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,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
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,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
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,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
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,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"marker": {
"line": {
"color": "#283442"
}
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"line": {
"color": "#283442"
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
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,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#506784"
},
"line": {
"color": "rgb(17,17,17)"
}
},
"header": {
"fill": {
"color": "#2a3f5f"
},
"line": {
"color": "rgb(17,17,17)"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#f2f5fa",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"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"
]
],
"sequential": [
[
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,
"#f0f921"
]
],
"sequentialminus": [
[
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,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#f2f5fa"
},
"geo": {
"bgcolor": "rgb(17,17,17)",
"lakecolor": "rgb(17,17,17)",
"landcolor": "rgb(17,17,17)",
"showlakes": true,
"showland": true,
"subunitcolor": "#506784"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "dark"
},
"paper_bgcolor": "rgb(17,17,17)",
"plot_bgcolor": "rgb(17,17,17)",
"polar": {
"angularaxis": {
"gridcolor": "#506784",
"linecolor": "#506784",
"ticks": ""
},
"bgcolor": "rgb(17,17,17)",
"radialaxis": {
"gridcolor": "#506784",
"linecolor": "#506784",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "rgb(17,17,17)",
"gridcolor": "#506784",
"gridwidth": 2,
"linecolor": "#506784",
"showbackground": true,
"ticks": "",
"zerolinecolor": "#C8D4E3"
},
"yaxis": {
"backgroundcolor": "rgb(17,17,17)",
"gridcolor": "#506784",
"gridwidth": 2,
"linecolor": "#506784",
"showbackground": true,
"ticks": "",
"zerolinecolor": "#C8D4E3"
},
"zaxis": {
"backgroundcolor": "rgb(17,17,17)",
"gridcolor": "#506784",
"gridwidth": 2,
"linecolor": "#506784",
"showbackground": true,
"ticks": "",
"zerolinecolor": "#C8D4E3"
}
},
"shapedefaults": {
"line": {
"color": "#f2f5fa"
}
},
"sliderdefaults": {
"bgcolor": "#C8D4E3",
"bordercolor": "rgb(17,17,17)",
"borderwidth": 1,
"tickwidth": 0
},
"ternary": {
"aaxis": {
"gridcolor": "#506784",
"linecolor": "#506784",
"ticks": ""
},
"baxis": {
"gridcolor": "#506784",
"linecolor": "#506784",
"ticks": ""
},
"bgcolor": "rgb(17,17,17)",
"caxis": {
"gridcolor": "#506784",
"linecolor": "#506784",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"updatemenudefaults": {
"bgcolor": "#506784",
"borderwidth": 0
},
"xaxis": {
"automargin": true,
"gridcolor": "#283442",
"linecolor": "#506784",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "#283442",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "#283442",
"linecolor": "#506784",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "#283442",
"zerolinewidth": 2
}
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# plot the points in 3d scatter plot\n",
"fig_3pts = px.scatter_3d(\n",
" A_df,\n",
" x = 'x', \n",
" y = 'y',\n",
" z = 'z',\n",
" template = 'plotly_dark', \n",
")\n",
"\n",
"# camera_topView = dict(\n",
"# up = dict(x=0, y=1, z=0), \n",
"# eye = dict(x=0., y=0., z=2.0), \n",
"# )\n",
"\n",
"fig_3pts.update_layout(\n",
" height=800, \n",
" scene=dict(\n",
" aspectmode ='cube', \n",
" aspectratio = dict(x=1, y=1, z=1), \n",
" xaxis = dict(range=[0, 2], nticks=5, ticks='outside', tickwidth=5, tickcolor='yellow'), \n",
" yaxis = dict(range=[-1, 1], nticks=5), \n",
" zaxis = dict(range=[-1, 1], nticks=5), \n",
" ),\n",
"# scene_camera=camera_topView,\n",
")\n",
"\n",
"fig_3pts.update_traces(\n",
" marker_size = 5,\n",
")\n",
"\n",
"fig_3pts.update_scenes(\n",
" xaxis_autorange='reversed', \n",
" yaxis_autorange='reversed', \n",
" camera_projection_type='orthographic', \n",
")\n",
"\n",
"fig_3pts.show()\n",
"fig_3pts.write_html('./fig_3pts.html')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "81684d8f5a970c8c0e02f404d54936361cc66a1d7985f3e531d0b89818a6c4f6"
},
"kernelspec": {
"display_name": "Python 3.8.10 64-bit ('pytorch3d': conda)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment