I am adding line charts to an OHLV plotly, but I do not manage to color them.
JavaScript
x
26
26
1
def chart_ema(histo_df:pd.DataFrame,
2
):
3
# https://plotly.com/python/ohlc-charts/
4
# THAT PARTS ADD THE OHLC CHART
5
6
fig = go.Figure(data=go.Ohlc(x=histo_df.index,
7
open=histo_df['open'],
8
high=histo_df['high'],
9
low=histo_df['low'],
10
close=histo_df['close'],
11
name='ohlc'
12
))
13
# THAT PARTS ADD THE 2 EMA LINE CHARTS
14
for column_name in histo_df.columns:
15
if 'ema' in column_name and 'cloud' not in column_name:
16
fig.add_scatter(x=histo_df.index,
17
y=histo_df[column_name],
18
name=column_name,
19
color='b'
20
# fillcolor='orange', # does not work either
21
22
)
23
24
fig.show()
25
26
The above code fail when I use ‘color’ as a ‘figg.add_scatter’ attribute : Error given is:
JavaScript
1
6
1
2
3
Did you mean "fill"?
4
5
Bad property path:
6
WHEN I do not specidy the color of the the scatter lines, the program works fine.
Bellow is a data sample:
JavaScript
1
13
13
1
trade_datetime,open,high,low,close,volume,ema_160,ema_50,cloud_50_160
2
2022-04-04 08:00:00+00:00,452.530000,453.290000,452.100000,452.430000,0,452.43,452.43,0.0
3
2022-04-04 08:15:00+00:00,452.450000,452.700000,452.310000,452.700000,0,452.43335403726707,452.4405882352941,0.007234198027049388
4
2022-04-04 12:00:00+00:00,453.440000,454.160000,452.160000,453.630000,0,452.61217633267256,452.9215287670084,0.30935243433583537
5
2022-04-04 12:15:00+00:00,453.630000,454.000000,453.610000,453.680000,0,452.6254412229499,452.9512727369297,0.3258315139797787
6
2022-04-04 12:30:00+00:00,453.710000,453.870000,453.480000,453.490000,0,452.6361810835343,452.97239929626585,0.33621821273152364
7
2022-04-04 12:45:00+00:00,453.460000,453.460000,452.860000,452.970000,0,452.6403279023724,452.9723052062162,0.3319773038438143
8
2022-04-04 13:00:00+00:00,452.960000,453.430000,452.950000,453.430000,0,452.65013749364726,452.99025402165876,0.3401165280114924
9
2022-04-04 13:15:00+00:00,,,,,0,452.65013749364726,452.99025402165876,0.3401165280114924
10
2022-04-04 13:30:00+00:00,453.000000,453.410000,452.260000,453.190000,3501932,452.6569271560714,452.99839388973334,0.3414667336619459
11
2022-04-04 13:45:00+00:00,453.190000,454.700000,452.650000,454.520000,3078587,452.6800709181078,453.05806471758694,0.3779937994791567
12
2022-04-04 14:00:00+00:00,454.500000,454.680000,454.000000,454.140000,2709000,452.6982066831002,453.1004935521914,0.40228686909119915
13
I would like to pick the colors of the lines ’ema_160′ and ’ema_50′ in the bellow example:
Thank you
I use: * Pycharm * Windows 10 * Python 3.7.9 And I tried the bellow urls without success:
pandas: Using color in a scatter plot
Advertisement
Answer
The color specification in graph_objects is done in the form of a dictionary with various settings. If you don’t need it, just use a single color.
JavaScript
1
31
31
1
def chart_ema(histo_df:pd.DataFrame,):
2
# https://plotly.com/python/ohlc-charts/
3
# THAT PARTS ADD THE OHLC CHART
4
5
fig = go.Figure(data=go.Ohlc(x=histo_df.index,
6
open=histo_df['open'],
7
high=histo_df['high'],
8
low=histo_df['low'],
9
close=histo_df['close'],
10
name='ohlc'
11
))
12
#fig.update_layout(rangeslider=dict(visible=False))
13
# THAT PARTS ADD THE 2 EMA LINE CHARTS
14
for column_name in histo_df.columns:
15
if 'ema' in column_name and 'cloud' not in column_name:
16
fig.add_scatter(x=histo_df.index,
17
y=histo_df[column_name],
18
name=column_name,
19
line=dict(color='royalblue' if column_name == 'ema_50' else 'red', dash='dash'),
20
mode='lines'
21
# fillcolor='orange', # does not work either
22
)
23
24
fig.update_layout(autosize=False,
25
width=800,
26
height=600
27
)
28
fig.show()
29
30
chart_ema(histo_df)
31