Skip to content

Instantly share code, notes, and snippets.

@Miladiouss
Created April 9, 2022 01:37
Show Gist options
  • Save Miladiouss/e2f4fef284ebf8461752a769e6ec5864 to your computer and use it in GitHub Desktop.
Save Miladiouss/e2f4fef284ebf8461752a769e6ec5864 to your computer and use it in GitHub Desktop.
An advanced example of generating publication quality plots for scientific journals in Python using Plotly.
import plotly.graph_objects as go
import numpy as np
from scipy.special import gamma
from pathlib import Path
here = Path(__file__).parent
filename = Path(__file__).stem
xdata = np.linspace(-5, 5, 1000)
ydata = gamma(xdata)
trace0 = go.Scatter(
x=xdata,
y=ydata,
name='Γ(x)', mode='lines',
line=dict(color='black', width=.8))
fig = go.Figure((trace0,))
# Styling
style_dict = {
'layout.plot_bgcolor': 'rgba(0, 0, 0, 0)',
'layout.font.family': 'Times New Roman',
'layout.xaxis.linecolor': 'black',
'layout.xaxis.ticks': 'inside',
'layout.xaxis.mirror': True,
'layout.xaxis.showline': True,
'layout.yaxis.linecolor': 'black',
'layout.yaxis.ticks': 'inside',
'layout.yaxis.mirror': True,
'layout.yaxis.showline': True,
'layout.autosize': False,
'layout.showlegend': True,
'layout.legend.bgcolor': 'rgba(0, 0, 0, 0)',
'layout.legend.xanchor': 'right',
'layout.legend.x': 1,
'layout.legend.font.family': 'monospace',
# Specialized:
# 'layout.xaxis.range': (2.3, 2.5),
'layout.yaxis.range': (-50, +50),
'layout.xaxis.title': r'$x$',
'layout.yaxis.title': r'$y$',
'layout.title': 'Advanced Example of a Line Plot with Plotly',
}
fig.update(**style_dict)
fig.add_annotation(dict(
xref='paper',
yref='paper',
x=0.5, y=-0.25,
showarrow=False,
text='The figure above, is an example of publication quality plotting for'
' scientific journals. This plot was generated using Plotly.'))
# Export figure
if True:
fig.write_image(here / f'{filename}.png')
fig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment