'Plotly plot histogram and line chart on same figure
Hello I am trying to plot a histogram and a line chart on the same figure to create a MACD chart. However the histogram data needs to be scaled down so it doesn't overtake the lines. Is there a way to scale the histogram down without scaling the data in my dataframe?
t.head()
Date macd macds macdh
index
0 2020-03-02 0.000000 0.000000 0.000000
1 2020-02-28 0.005048 0.002804 0.002244
2 2020-02-27 -0.000080 0.001622 -0.001702
3 2020-02-26 0.016184 0.006555 0.009629
4 2020-02-25 0.023089 0.011473 0.011615
fig = go.Figure()
fig.add_trace(go.Histogram(
x=t['Date'],
y=t['macdh'],
))
fig.add_trace(go.Scatter(
x=t['Date'],
y=t['macd'],
line_color='dimgray',
opacity=0.8))
fig.add_trace(go.Scatter(
x=t['Date'],
y=t['macds'],
line_color='deepskyblue',
opacity=0.8
))
fig.show()
Ideally something like this in Python
Solution 1:[1]
In order to make absolutely sure that different data categories do not interfere with each other, I prefer setting them up with individual subplots rather than plots with a mix of secondary y-axes. Here's an example:
Complete code:
import plotly.graph_objects as go
import plotly.io as pio
from plotly.subplots import make_subplots
import pandas as pd
pio.templates.default = "plotly_white"
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = make_subplots(vertical_spacing = 0, rows=3, cols=1, row_heights=[0.6, 0.2, 0.2])
fig.add_trace(go.Candlestick(x=df['Date'],
open=df['AAPL.Open'],
high=df['AAPL.High'],
low=df['AAPL.Low'],
close=df['AAPL.Close']))
fig.add_trace(go.Scatter(x=df['Date'], y = df['mavg']), row=2, col=1)
fig.add_trace(go.Scatter(x=df['Date'], y = df['mavg']*1.1), row=2, col=1)
fig.add_trace(go.Bar(x=df['Date'], y = df['AAPL.Volume']), row=3, col=1)
fig.update_layout(xaxis_rangeslider_visible=False,
xaxis=dict(zerolinecolor='black', showticklabels=False),
xaxis2=dict(showticklabels=False))
fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=False)
fig.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | vestland |