Outdoor Technologist

Random thoughts spewed in the digital realm

Julia – Modify 3D Background colors

Julia allows for you to customize plots to meet customer’s requests. This came in handy, but it was hard to find for this novice programmer.

This link helped me solve the problem: https://plotly.com/julia/3d-axes/#background-and-grid-color

My task required printing from 3 dimensional temperature data, rather than an equations, so I couldn’t follow the exact method shown here.

My data was saved in a dataframe, so other manipulations were required for success.

Largest difference: The example created a trace & layout and then uses the plot command to create the visualization.

The key to my success was learning how to use “Layout” in the plot command – once I found this solution and then figured out the proper syntax, it was quite easy to implement.

using PlotlyJS

N = 50
trace = mesh3d(x=(30 .* randn(N)),
                   y=(25 .* randn(N)),
                   z=(30 .* randn(N)),
                   opacity=0.5)


# xaxis.backgroundcolor is used to set background color
layout = Layout(scene = attr(
                    xaxis = attr(
                         backgroundcolor="rgb(200, 200, 230)",
                         gridcolor="white",
                         showbackground=true,
                         zerolinecolor="white",),
                    yaxis = attr(
                        backgroundcolor="rgb(230, 200,230)",
                        gridcolor="white",
                        showbackground=true,
                        zerolinecolor="white"),
                    zaxis = attr(
                        backgroundcolor="rgb(230, 230,200)",
                        gridcolor="white",
                        showbackground=true,
                        zerolinecolor="white",),),
                    width=700,
                    margin=attr(
                    r=10, l=10,
                    b=10, t=10)
                  )
plot(trace, layout)

The code above resulted in the plot below.

Notice that the y and x planes are the same color – this accomplishes the task I was assigned.

The Layout command

The key to success was learning how Layout can powerfully modify the plot.

Layout(scene = attr( 

Use Layout to add this visual.

Reasons I couldn’t use the method in the example:

The data I was working with was in a dataframe, and I am displaying the temp data in 4 dimensions – x,y,z plus a color for temperature range.

I didn’t see a quick way to split my data into trace & layout, and I quickly learned that I could combine them and get the desired result.

My resulting code is:

pW = plot([scatter(
           subdf,
           x = :xW,  # collected data
           y = :yW,
           z = :zW,
           type="scatter3d",
           name = subdf[1, :tempRange],
           marker_color = tempRangeW_map[ subdf[1, :tempRange] ], #dictionary 
           mode = "markers",
           )
        for subdf in groupby(df_w, :tempRange)], #df_w is the original dataframe

        Layout(margin=attr(l=0, r=0, b=0, t=0),
                scene= attr(
                            xaxis_title="x",
                            yaxis_title="y",
                            zaxis_title="z (inch)",
                zaxis_tickvals=[-2,-4,-6,-8],
                xaxis = attr(
                            backgroundcolor="rgb(200, 200, 230)",
                            gridcolor="white",
                            showbackground=true,
                            zerolinecolor="white",),
                yaxis = attr(
                        backgroundcolor="rgb(200, 200,230)",
                        gridcolor="white",
                        showbackground=true,
                        zerolinecolor="white"),
                zaxis = attr(
                        backgroundcolor="rgb(230, 230,200)",
                        gridcolor="white",
                        showbackground=true,
                        zerolinecolor="white",),
                )
            )
        )


name = "eye = (x:-0.45, y:-0.93, z:2.5), z axis in inches" #Title

camera = attr(
            eye=attr(x=-.45, y=-.93, z=2.5) # view vector
            )

relayout!(pW, scene_camera=camera, title=name) # modifies plot output

pW    # display new plot

I’m sure someone well versed in Julia could improve upon this, so please share any improvements you have.

julia

John • 2023-02-09


Previous Post

Next Post