コード例 #1
0
def formatted_fig_json(fig: go.Figure) -> dict:
    """Formats figure with styling and returns as JSON.

    :param fig: Plotly figure.
    :type fig: go.Figure
    :return: Formatted plotly figure as a JSON dict.
    :rtype: dict
    """
    fig.update_xaxes(title_font=dict(size=16, color="black"),
                     tickfont={"size": 14})
    fig.update_yaxes(title_font=dict(size=16, color="black"),
                     tickfont={"size": 14})
    fig.update_layout(
        hoverlabel_align="right",
        font_family="Roboto",
        title_font_size=20,
        plot_bgcolor="white",
        paper_bgcolor="white",
    )
    return json.loads(fig.to_json())
コード例 #2
0
 def write_file(self, f: TextIO, chart: PFigure):
     json.dump(chart.to_json(), f)
コード例 #3
0
ファイル: plots.py プロジェクト: dmitrydavydov96/quantmodels
class TonextyPlot(object):
    def __init__(self, y, upper, lower, x=None):

        self.fig = Figure()
        self.colors = ['rgb(144, 144, 144)', 'rgb(190, 190, 190)']
        self.config = {'displayModeBar': False}

        self.y = y
        self.upper, self.lower = upper, lower

        if isinstance(x, type(None)):
            self.x = array(list(range(0, len(y))))

    def add_traces(self):

        self.fig.add_trace(
            Scatter(x=self.x,
                    y=self.lower,
                    line_color='rgb(220, 220, 220)',
                    showlegend=False,
                    mode='lines',
                    name='Нижняя граница (95%-й интервал)'))

        self.fig.add_trace(
            Scatter(x=self.x,
                    y=self.y,
                    name='Прогноз',
                    mode='lines',
                    line=dict(color=self.colors[0]),
                    showlegend=True))

        self.fig.add_trace(
            Scatter(x=self.x,
                    y=self.upper,
                    fill='tonexty',
                    fillcolor='rgba(250, 250, 250, 0.4)',
                    line_color='rgb(220, 220, 220)',
                    showlegend=False,
                    mode='lines',
                    name='Верхняя граница (95%-й интервал)'))

        return self

    def update_layout(self):

        title = 'Результат построения 95%-й доверительного интервала прогноза'
        self.fig.update_layout(
            title={
                'text': title,
                'y': 0.98,
                'x': 0.08
            },
            height=320,
            margin={
                't': 50,
                'b': 0
            },
            legend_orientation="h",
            legend=dict(x=-0.005, y=1.125),
        )
        return self

    def to_json(self):

        self.add_traces()
        self.update_layout()

        return self.fig.to_json()

    def plot(self, anaconda=False):

        self.add_traces()

        if not anaconda:
            self.update_layout()
        else:
            margin_bottom = 50
            self.update_layout()
            self.fig.layout.height += margin_bottom
            self.fig.layout.margin = {'t': 50, 'b': margin_bottom}

        return self.fig.show(config=self.config)