<jupyter_start><jupyter_text>3D scatter plot with adjustable settingsThe idea is that some settings can be offered as selectable settings via a graphical user interface to enable keying in a good combination faster.Note that the use of these approaches to add adjustable settings is on another notebook page as it doesn't play well with the use of `%matplotlib notebook` that is encouraged on [the introductory notebook page](index.ipynb).The example here works with with the use of `%matplotlib notebook`; however, it is extra 'choppy'/'laggy'.------If you haven't used one of these notebooks before, they're basically web pages in which you can write, edit, and run live code. They're meant to encourage experimentation, so don't feel nervous. Just try running a few cells and see what happens!.    Some tips:            Code cells have boxes around them.        To run a code cell either click the Play icon on the menu bar above, or click on the cell and then hit Shift+Enter. The Shift+Enter combo will also move you to the next cell, so it's a quick way to work through the notebook.        While a cell is running a * appears in the square brackets next to the cell. Once the cell has finished running the asterisk will be replaced with a number.        In most cases you'll want to start from the top of notebook and work your way down running each cell in turn. Later cells might depend on the results of earlier ones.        To edit a code cell, just click on it and type stuff. Remember to run the cell once you've finished editing.     ------- Matplotlib example with colorbar legend and adjustable optionsThe palette, view angles, opacity of spheres, size of spheres, view, and color gradient atrribute is adjustable using widgets.This is based on the Matplotlib examples in the first notebook [here](https://github.com/fomightez/3Dscatter_plot-binder) combined with [ipywidgets adjusting plot options](https://github.com/binder-examples/jupyter-extension).**When this notebook is opened in active form, the old plot will not be seen. You need to re-run the cells to see the output & widgets for adjusting the settings.**<jupyter_code>%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from ipywidgets import interact
from ipywidgets.widgets import FloatSlider, IntSlider, Dropdown
plt.ion()

!curl -OL https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv
!curl -OL https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv
red_wine   = pd.read_csv('winequality-red.csv',   sep=';')
white_wine = pd.read_csv('winequality-white.csv', sep=';')
wines = pd.concat([red_wine,white_wine], ignore_index=True)
print("red wines:",len(red_wine))
print("white wines:",len(white_wine))
print("wines:",len(wines))
print("\nADJUSTABLE SETTINGS:")

xs = wines['residual sugar']
ys = wines['fixed acidity']
zs = wines['alcohol']

cmaps = ['coolwarm', 'viridis', 'magma','plasma','RdYlBu','YlGnBu','hsv']
v_angles = ["30, 185","50, 185","230,110","1,140"]

view_widget = Dropdown(options=v_angles)
elevation_widget= IntSlider(value=30, min=0, max=360,step=5, continuous_update=False)
azimuth_widget= IntSlider(value=185, min=0, max=360,step=5, continuous_update=False)
def update_elevation_and_azimuth(*args):
    view_nums = view_widget.value.split(",")
    view_nums = [int(x) for x in view_nums]
    elevation_widget.value, azimuth_widget.value = view_nums
view_widget.observe(update_elevation_and_azimuth, 'value')

def plot_data(opacity, cmap, coloring,marker_size,view_choice,elevation,azimuth):
    fig = plt.figure(figsize=(9.5, 6))
    ax = fig.add_subplot(111, projection='3d')
    #norm = mpl.colors.Normalize(vmin=min(wines[coloring],), vmax=max(wines[coloring],))
    g = ax.scatter(xs, ys, zs, s=marker_size, alpha=opacity, edgecolors='w', marker='o', depthshade=False, c=wines[coloring], cmap=cmap)
    ax.view_init(elevation,azimuth)
    ax.set_xlabel('Residual Sugar')
    ax.set_ylabel('Fixed Acidity')
    ax.set_zlabel('Alcohol')
    clb = fig.colorbar(g)
    clb.solids.set_edgecolor("face") #based on https://stackoverflow.com/a/5495912/8508004; 
    # addition of `.solids.set_edgecolor("face")` based on https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.colorbar.html to 
    # avoid banding seen with just `fig.colorbar(g)` along with ipywidgets use; note it needed further adjusting to 
    # clb.solids.set_edgecolor("face") when added title to colorbar based on https://stackoverflow.com/a/33740567/8508004
    clb.ax.set_title(coloring) #adding title based on https://stackoverflow.com/a/33740567/8508004
    plt.show()

p = interact(plot_data,
             opacity=FloatSlider(value=0.75, min=0, max=1.0, step=0.05, readout_format='.3f', continuous_update=False),
             cmap=Dropdown(value='magma',options=cmaps),
             coloring=Dropdown(value='alcohol',options=['residual sugar','fixed acidity','alcohol']),
             marker_size= IntSlider(value=50, min=10, max=180,step=10, continuous_update=False),
             view_choice=view_widget,
             elevation= elevation_widget,
             azimuth= azimuth_widget
            )<jupyter_output>% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 84199  100 84199    0     0   201k      0 --:--:-- --:--:-- --:--:--  201k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  258k  100  258k    0     0   692k      0 --:--:-- --:--:-- --:--:--  692k
red wines: 1599
white wines: 4898
wines: 6497

ADJUSTABLE SETTINGS:<jupyter_text>------- Plotly examples with colorbar legend and adjustable optionsYou shouldn't need to run the above code first. Plotly Express exampleThe palette, view angles, opacity of spheres, view, and color gradient atrribute is adjustable using widgets.This is based on the Plotly examples [here](https://github.com/fomightez/Plotly3d-scatter-plots.ipynb) combined with [ipywidgets adjusting plot options](https://github.com/binder-examples/jupyter-extension).**When this notebook is opened in active form, the old plot will not be seen. You need to re-run the cells to see the output & widgets for adjusting the settings.**<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from ipywidgets import interact
from ipywidgets.widgets import FloatSlider, IntSlider, Dropdown
#plt.ion()
import plotly.express as px

df = px.data.iris()

cmaps = ['viridis', 'magma','plasma','icefire','RdYlBu','YlGnBu','hsv']
v_angles = ["1.5, 1.5, 0.7","1, 1, 0.1","2, 2, 0.1","1.5, 0.01, 0.01","1.0, 0.3, 0.99","1.2, 0.9, 0.5","1.0, 0.6, 1.2"]
view_widget = Dropdown(options=v_angles)
camera_x_widget= FloatSlider(value=1.5, min=0, max=2.5, step=0.05, readout_format='.3f', continuous_update=False)
camera_y_widget= FloatSlider(value=1.5, min=0, max=2.5, step=0.05, readout_format='.3f', continuous_update=False)
camera_z_widget= FloatSlider(value=0.7, min=0, max=2.5, step=0.05, readout_format='.3f', continuous_update=False)
def update_camera_eye(*args):
    view_nums = view_widget.value.split(",")
    view_nums = [float(x) for x in view_nums]
    camera_x_widget.value,camera_y_widget.value,camera_z_widget.value = view_nums
view_widget.observe(update_camera_eye, 'value')
def plot_data(opacity, cmap, coloring,marker_size,view_choice,camera_x,camera_y,camera_z):
    fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', 
        color=coloring, opacity=opacity, color_continuous_scale=cmap)
    camera = dict(
    eye=dict(x= camera_x, y=camera_y, z=camera_z)
    )
    fig.update_layout(scene_camera=camera)
    fig.update_traces(marker=dict(size=marker_size), #adjusting size of marker in express based on https://plotly.com/python/marker-style/
                  selector=dict(mode='markers'))
    fig.show()

p = interact(plot_data,
             opacity=FloatSlider(value=0.75, min=0, max=1.0, step=0.05, readout_format='.3f', continuous_update=False),
             cmap=Dropdown(value='magma',options=cmaps),
             coloring=Dropdown(value='petal_length',options=['petal_length','sepal_width','petal_width']),
             marker_size= IntSlider(value=8, min=3, max=15, continuous_update=False),
             view_choice=view_widget,
             camera_x = camera_x_widget,
             camera_y = camera_y_widget,
             camera_z = camera_z_widget,
            )<jupyter_output><empty_output><jupyter_text>Plotly Graph Objects exampleThe palette, view angles, opacity of spheres, size of spheres, view, and color gradient atrribute is adjustable using widgets.This is based on the Plotly examples [here](https://github.com/fomightez/Plotly3d-scatter-plots.ipynb) combined with [ipywidgets adjusting plot options](https://github.com/binder-examples/jupyter-extension).**When this notebook is opened in active form, the old plot will not be seen. You need to re-run the cells to see the output & widgets for adjusting the settings.**<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from ipywidgets import interact
from ipywidgets.widgets import FloatSlider, IntSlider, Dropdown
#plt.ion()
import plotly.express # only using as source of data in this cell
import plotly.graph_objects as go

df = plotly.express.data.iris()

cmaps = ['viridis', 'magma','plasma','icefire','RdYlBu','YlGnBu','hsv']
v_angles = ["1.5, 1.5, 0.7","1, 1, 0.1","2, 2, 0.1","1.5, 0.01, 0.01","1.0, 0.3, 0.99","1.2, 0.9, 0.5","1.0, 0.6, 1.2"]
view_widget = Dropdown(options=v_angles)
camera_x_widget= FloatSlider(value=1.5, min=0, max=2.5, step=0.05, readout_format='.3f', continuous_update=False)
camera_y_widget= FloatSlider(value=1.5, min=0, max=2.5, step=0.05, readout_format='.3f', continuous_update=False)
camera_z_widget= FloatSlider(value=0.7, min=0, max=2.5, step=0.05, readout_format='.3f', continuous_update=False)
def update_camera_eye(*args):
    view_nums = view_widget.value.split(",")
    view_nums = [float(x) for x in view_nums]
    camera_x_widget.value,camera_y_widget.value,camera_z_widget.value = view_nums
view_widget.observe(update_camera_eye, 'value')
def plot_data(opacity, cmap, coloring, marker_size,view_choice,camera_x,camera_y,camera_z):
    fig = go.Figure(data=[go.Scatter3d(x=df['sepal_length'], y=df['sepal_width'], z=df['petal_width'], 
        mode='markers',
        marker=dict(
            size=marker_size,
            color=df[coloring],                # set color to an array/list of desired values
            colorbar=dict(
                title=coloring #based on https://plotly.com/python/colorscales/#color-scale-for-scatter-plots-with-graph-objects
            ),
            colorscale= cmap,   # choose a colorscale
            opacity=opacity
        )
    )])
    camera = dict(
    eye=dict(x= camera_x, y=camera_y, z=camera_z)
    )
    fig.update_layout(scene_camera=camera)
    # tight layout
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
    # add axis labels based on https://plotly.com/python/3d-axes/#set-axes-title , that you
    # can arrive at by going to https://plotly.com/python/axes/ and selecting `3D axes` 
    fig.update_layout(scene = dict(
                        xaxis_title="sepal_length",
                        yaxis_title="sepal_width",
                        zaxis_title="petal_width"),
                        )
    fig.show()


p = interact(plot_data,
             opacity=FloatSlider(value=0.85, min=0, max=1.0, step=0.05, readout_format='.3f', continuous_update=False),
             cmap=Dropdown(value='viridis',options=cmaps),
             coloring=Dropdown(value='petal_length',options=['petal_length','sepal_width','petal_width']),
             marker_size= IntSlider(value=9, min=3, max=18, continuous_update=False),
             view_choice=view_widget,
             camera_x = camera_x_widget,
             camera_y = camera_y_widget,
             camera_z = camera_z_widget,
            )<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup