Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to change axis titles in plotly surface plot in python

I am working with plotly version 3.9.0 and python 3.7 . I am trying to render a simple surface plot in plotly using python. I have successfully rendered the plot with the intended color scheme but am having trouble with changing the axis titles of the X,Y,Z axes and also with controlling the tick frequency and tick labels for each of the axes. I have tried some of the solutions from other related questions on SO without much success. Hence, I wanted to post a snippet of my code below to seek help from the SO community about how I can go about changing the axis labels and also set the axis ticks and tick labels.

import plotly.plotly as py
import plotly.graph_objs as go

data = [
    go.Surface(
        z = p    # p is a 2D square matrix.
    )
]
data[0]['surfacecolor'] = u   #Setting surface color with another 2D square matrix `u` of the same shape as `p` 

layout = go.Layout(
         xaxis = go.layout.XAxis(
        title = go.layout.xaxis.Title(
                  text='x Axis'),
         font=dict(
         family='Courier New, monospace',
         size=18,
         color='#7f7f7f'
         )
    ),
    title = go.layout.Title(
        text='Mean Pressure Field 0.25 Granularity, Colored by Mean Particle Velocity (X-direction)'
    ),
    autosize=False,
    width=1000,
    height=1000,
    margin=dict(
        l=65,
        r=50,
        b=65,
        t=90
    ),
)

fig = go.Figure(data=data, 
                layout=layout)

py.iplot(fig, filename='saveplot.png')

Here is the figure that I have been able to produce with the above code (with the new title for the X axis specified). Notice that the x axis label does not match the new title specified.

3D Surface Plot Plotly Python

like image 262
Nikhil Avatar asked May 21 '19 19:05

Nikhil


People also ask

How do you add labels in Plotly?

As a general rule, there are two ways to add text labels to figures: Certain trace types, notably in the scatter family (e.g. scatter , scatter3d , scattergeo etc), support a text attribute, and can be displayed with or without markers. Standalone text annotations can be added to figures using fig.

How do you change your title color in Plotly?

@alexpetit12 You can set the font color for the entire title not only for substrings of it. layout=go. layout(title = dict(text ='Your title', font =dict(family='Sherif', size=14, color = 'red')), ... ) for more title attributes see https://plot.ly/python/reference/#layout-title.


1 Answers

Replace this block:

xaxis = go.layout.XAxis(
        title = go.layout.xaxis.Title(
                  text='x Axis'),
         font=dict(
         family='Courier New, monospace',
         size=18,
         color='#7f7f7f'
         )
    )

By this:

scene = dict(
                    xaxis = dict(
                        title='X AXIS TITLE'),
                        font=dict(
                                  family='Courier New, monospace',
                                  size=18,
                                  color='#7f7f7f')
                    yaxis = dict(
                        title='Y AXIS TITLE'),
                    zaxis = dict(
                        title='Z AXIS TITLE'),),

See this source. Feel free to ask any more questions!

like image 108
Mike_H Avatar answered Oct 01 '22 22:10

Mike_H