Skip to content

3D Filled Line Plots with Plotly #1326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MdoubleDash opened this issue Apr 27, 2019 · 1 comment
Closed

3D Filled Line Plots with Plotly #1326

MdoubleDash opened this issue Apr 27, 2019 · 1 comment

Comments

@MdoubleDash
Copy link

I am trying to make 3d filled line plot following this example: https://plot.ly/python/3d-filled-line-plots/

Clearly there is a bug when you want to fill under the curves as it just considers first and last datapoints and draws a straight line instead of filling exactly under the curve. I was wondering if anybody have found/is working for a fix?

I had the same issue doing this in R as well.

See the code and the plot below:

import plotly
import pandas as pd

plotly.tools.set_credentials_file(username='YourUsername', api_key='YourKey')

# The datasets' url. Thanks Jennifer Bryan!
url_csv = 'http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt'

df = pd.read_csv(url_csv, sep='\t')
df.head()

countries = ['China', 'India', 'United States', 'Bangladesh', 'South Africa']
fill_colors = ['#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', '#a6d854']
gf = df.groupby('country')

data = []

for country, fill_color in zip(countries[::-1], fill_colors):
    group = gf.get_group(country)
    years = group['year'].tolist()
    length = len(years)
    country_coords = [country] * length
    pop = group['pop'].tolist()
    zeros = [0] * length
    
    data.append(dict(
        type='scatter3d',
        mode='lines',
        x=years + years[::-1] + [years[0]],  # year loop: in incr. order then in decr. order then years[0]
        y=country_coords * 2 + [country_coords[0]],
        z=pop + zeros + [pop[0]],
        name='',
        surfaceaxis=1, # add a surface axis ('1' refers to axes[1] i.e. the y-axis)
        surfacecolor=fill_color,
        line=dict(
            color='black',
            width=4
        ),
    ))

layout = dict(
    title='Population from 1957 to 2007 [Gapminder]',
    showlegend=False,
    scene=dict(
        xaxis=dict(title=''),
        yaxis=dict(title=''),
        zaxis=dict(title=''),
        camera=dict(
            eye=dict(x=-1.7, y=-1.7, z=0.5)
        )
    )
)

fig = dict(data=data, layout=layout)

plotly.plotly.iplot(fig, filename='filled-3d-lines')
filled-3d-lines
@MdoubleDash
Copy link
Author

MdoubleDash commented Apr 29, 2019

Look at these issues:

plotly/plotly.js#2352
plotly/plotly.py#1206

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant