Save_as saves blank images?

I think I am doing something wrong here, but would love to know what it is. At least by me, this code displays the image I want, but saves a blank image (can see grid, but no bars). Also, please forgive in advance if this code does not show up as formatted code, it’s my first post here, and couldn’t figure out how to do that :slight_smile:

def score_bar_test(values, groups=[0,1], colors=['#1f77b4', '#ff7f0e']):
    traces = []
    for i in range(len(values)):
        traces.append(go.Bar(
                x=values[i],
                y=i,
                orientation = 'h',
                name=groups[i]
        ))
        
    layout = go.Layout(
        barmode='group',
        showlegend=False,
        width=300,
        height=400,
        xaxis=dict(
        ticks='',
        showticklabels=False
    ),
    yaxis=dict(
        ticks='',
        showticklabels=False
    )    
    )
    
    fig = go.Figure(data=traces, layout=layout)
    return fig

values = [1, 1]
fig = score_bar_test(values)
py.image.save_as(fig, 'testh.pdf')
py.iplot(fig,filename='tryingbars')

image

Hi @Tali,

Your code shows up fine :slightly_smiling_face: If you want it to show up with Python syntax highlighting, then add python after the first set of triple backticks:

```python
def score_bar_test(values, groups=[0,1], colors=['#1f77b4', '#ff7f0e']):
    traces = []
    ...
```

In terms of your question itself, could you please add:

  1. The import statements your using so that someone can copy your code, paste it into a jupyter notebook cell, and run it.
  2. The version of plotly.py you have installed
  3. The code you’re running to save the image

Thanks!
-Jon

Thanks Jon!!
My plotly version is 2.7.0

I’ve added the import statements (sorry for leaving these out before).
The code I run to save appears at the bottom of the code below, but am repasting it here for convenience

py.image.save_as(fig, '/data/testh.pdf')

Thanks for the help!
Tali

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

def score_bar_test(values, groups=[0,1], colors=['#1f77b4', '#ff7f0e']):
    traces = []
    for i in range(len(values)):
        traces.append(go.Bar(
                x=values[i],
                y=i,
                orientation = 'h',
                name=groups[i]
        ))
        
    layout = go.Layout(
        barmode='group',
        showlegend=False,
        width=300,
        height=400,
        xaxis=dict(
        ticks='',
        showticklabels=False
    ),
    yaxis=dict(
        ticks='',
        showticklabels=False
    )    
    )
    
    fig = go.Figure(data=traces, layout=layout)
    return fig

values = [1, 1]
fig = score_bar_test(values)
py.image.save_as(fig, '/data/testh.pdf')
py.iplot(fig,filename='tryingbars')

Thanks @Tali,

Do you have a Student/Personal/Professional plot.ly plan (https://plot.ly/products/cloud/)? I don’t have one set up right now so I can’t test the PDF export. I get the full image when I save to a *.png format though (e.g. py.image.save_as(fig, '/data/test.png')). Do you?

-Jon