{"id":20857664,"url":"https://github.com/tracywong117/autoscale-plotly-candlestick","last_synced_at":"2025-10-18T02:31:54.287Z","repository":{"id":219248038,"uuid":"748555257","full_name":"tracywong117/autoscale-plotly-candlestick","owner":"tracywong117","description":"Solution to autoscale y-axis of Plotly candlestick when using rangeslider","archived":false,"fork":false,"pushed_at":"2024-01-26T09:06:08.000Z","size":71,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-19T07:14:17.186Z","etag":null,"topics":["plotly","plotly-dash","python-plotly"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tracywong117.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-26T08:39:36.000Z","updated_at":"2025-01-12T06:08:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"2b462847-152e-4f1d-9cc4-229e57423d86","html_url":"https://github.com/tracywong117/autoscale-plotly-candlestick","commit_stats":null,"previous_names":["tracywong117/autoscale-plotly-candlestick"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tracywong117%2Fautoscale-plotly-candlestick","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tracywong117%2Fautoscale-plotly-candlestick/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tracywong117%2Fautoscale-plotly-candlestick/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tracywong117%2Fautoscale-plotly-candlestick/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tracywong117","download_url":"https://codeload.github.com/tracywong117/autoscale-plotly-candlestick/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243230061,"owners_count":20257640,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["plotly","plotly-dash","python-plotly"],"created_at":"2024-11-18T04:42:15.003Z","updated_at":"2025-10-18T02:31:49.269Z","avatar_url":"https://github.com/tracywong117.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Autoscale y-axis of candlestick when using rangeslider\n\nWhen using Plotly Python to plot candlestick charts for stock prices, one may encounter the issue that the y-axis does not autoscale when using the rangeslider to select a specific range of data. This code snippet provides a solution by utilizing a Dash callback for relayout, which allows for obtaining the range selected by the user and adjust the y-axis accordingly.\n\n```Python\napp.layout = html.Div(\n    [\n        dcc.Graph(id=\"stock-chart\", figure=stockfig, style={\"width\": \"1300px\"}),\n        html.Div(id=\"range-output\"),\n    ]\n)\n\n\ndef find_min_max(df, start_date, end_date):\n    df[\"Date\"] = pd.to_datetime(df[\"Date\"])\n    filtered_df = df[(df[\"Date\"] \u003e= start_date) \u0026 (df[\"Date\"] \u003c= end_date)]\n    # print(filtered_df)\n    min_values = min(list(filtered_df[[\"Open\", \"High\", \"Low\", \"Close\"]].min()))\n    max_values = max(list(filtered_df[[\"Open\", \"High\", \"Low\", \"Close\"]].max()))\n    # print(min_values, max_values)\n    return min_values, max_values\n\n\n@app.callback(Output(\"stock-chart\", \"figure\"), [Input(\"stock-chart\", \"relayoutData\")])\ndef display_relayout_data(relayoutData):\n    global stockfig\n    start = end = 0\n\n    if not relayoutData:\n        raise PreventUpdate\n\n    if relayoutData.get(\"xaxis.range\", False):\n        start = relayoutData[\"xaxis.range\"][0]\n        end = relayoutData[\"xaxis.range\"][1]\n    elif relayoutData.get(\"xaxis.range[0]\", False):\n        start = relayoutData[\"xaxis.range[0]\"]\n        end = relayoutData[\"xaxis.range[1]\"]\n\n    if start != 0:\n        print(\"Start: \", start.split(\" \")[0])\n        print(\"End: \", end.split(\" \")[0])\n        start = start.split(\" \")[0]\n        end = end.split(\" \")[0]\n\n        ymin, ymax = find_min_max(df, start, end)\n\n        diff = ymax - ymin\n\n        ymin -= min(diff * 0.2, 2)\n        ymax += min(diff * 0.2, 2)\n\n        stockfig.update_layout(\n            xaxis_autorange=False,\n            yaxis_autorange=False,\n            xaxis_range=[start, end],\n            yaxis_range=[ymin, ymax],\n        )\n\n        return stockfig\n    else:\n        raise PreventUpdate\n```\n\n## Demo\n![Demo](Demo.png)\nRun autoscale-candlestick.py to see the demo.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftracywong117%2Fautoscale-plotly-candlestick","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftracywong117%2Fautoscale-plotly-candlestick","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftracywong117%2Fautoscale-plotly-candlestick/lists"}