Image does not contains bars in bar chart


Fig.show() looks like above img.
But when I save as png it looks like below image

Code:

data.append(
go.Bar(x=columnchart_df[x_axis_name], y=columnchart_df[x_axis_name], 
        visible=True,  # Set bar visibility
        width=0.5,
        marker_line_width=0.009,
        howlegend=True))

layout = {
                    "yaxis": dict(
                        title="Primary yaxis"
                    )
                }
fig = go.Figure(data=data, layout=layout)

import plotly.io as pio
img_bytes = pio.to_image(fig, format='png', scale=10)
img_base64 = base64.b64encode(img_bytes).decode('utf-8')
img_data = f'<img src="data:image/png;base64,{img_base64}">'

I gave same values from dataframe as X and Y.

Hi @shivamk ,

Welcome to the community !

Have you double check that your second image is generated from first figure ? because in the second image there is no title, but first image has.

May we know what method you use to export figure as png file ?

For other resources about export to static image:

Hey, thanks for your response @farispriadi
I will explain from start.
Code:
data =
data.append(
go.Bar(x=columnchart_df[x_axis_name], y=columnchart_df[x_axis_name], marker_color=‘red’,
visible=True, # Set bar visibility
width=0.5,
name=channels_with_unit.get(y), legendgroup=‘exh_1_t(c)’,
showlegend=True))
layout = {
“yaxis”: dict(
title=“Primary yaxis”
)
}
fig = go.Figure(data=data, layout=layout)
fig.update_layout(barmode=‘group’)
fig.update_layout(title=‘Column Chart’)
fig.update_layout(paper_bgcolor=‘rgba(0,0,0,0)’, plot_bgcolor=‘rgba(0,0,0,0)’)
fig.show()

From this code I get below response:

Code to export static image: which you send me
fig.write_image(“fig1.png”)
Downloaded image look like below image:

So I give one property: marker_line_width=0.009
Code:
go.Bar(x=columnchart_df[x_axis_name], y=columnchart_df[x_axis_name], marker_color=‘red’,
visible=True, # Set bar visibility
width=0.5,
name=channels_with_unit.get(y), legendgroup=‘exh_1_t(c)’,
marker_line_width=0.009,
showlegend=True))

I just added marker_line_width=0.009, remaining code is same
On fig.show() I got below response which was not faded like before: see below image

Code to export static image: which you send me
fig.write_image(“fig2.png”)
Downloaded image look like below image:

My data is integer values from 0 to 222k.
Bars are not visible in static image!

Hi @shivamk ,

I create your sample data by generating range from1-222k
here the code like you do :

import plotly.graph_objects as go

x = list(range(1,222_000))
y = list(range(1,222_000))

data = []
data.append(
    go.Bar(x=x, y=y, marker_color="red",
        visible=True, # Set bar visibility
        width=0.5,
        name='exh_1_t', legendgroup="exh_1_t(c)",
        marker_line_width=0.009,
        showlegend=True))
layout = {
    "yaxis": dict(
    title="Primary yaxis"
    )
}
fig = go.Figure(data=data, layout=layout)
fig.update_layout(barmode="group")
fig.update_layout(title="Column Chart")
fig.update_layout(paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)")

fig.write_image("image_large_bar.png")
fig.show()

But fortunately works in my machine (Python 3.11.4 , plotly 5.21.0, kaleido 0.2.1 ) , it has colored output not faded.

Here the static image output.

Have you checked the version of installed libraries?
Maybe in your case , the problem is from the engine like kaleido or orca.

Hey @farispriadi , this case generate when data not in series.
I have performed sampling on 222k data. I have provided values.
I was unable to attach data file. So I have provided values.
Please check on that data
X = [0,
498,
499,
500,
501,
999,
1000,
1002,
1499,
1500,
1997,
1999,
2000,
2497,
2499,
2500,
2501,
2997,
2999,
3000,
3215,
3494,
3499,
3500,
3501,
3998,
3999,
4000,
4010,
4459,
4499,
4500,
4554,
4894,
4999,
5000,
5026,
5498,
5499,
5500,
5575,
5892,
5999,
6000,
6208,
6498,
6499,
6500,
6609,
6946,
6999,
7000,
7001,
7253,
7499,
7500,
7633,
7999,
8000,
8017,
8282,
8499,
8500,
8556,
8998,
8999,
9000,
9116,
9498,
9499,
9500,
9547,
9792,
9999,
10000,
10001,
10371,
10499,
10500,
10821,
10997,
10999,
11000,
11033,
11436,
11499,
11500,
11645,
11995,
11999,
12000,
12145,
12499,
12500,
12625,
12997,
12999,
13000,
13344,
13499,
13500,
13675,
13999,
14000,
14257,
14352,
14499,
14500,
14530,
14989,
14999,
15000,
15235,
15499,
15500,
15513,
15872,
15999,
16000,
16001,
16466,
16499,
16500,
16665,
16845,
16999,
17000,
17162,
17307,
17499,
17500,
17504,
17781,
17999,
18000,
18132,
18499,
18500,
18606,
18858,
18999,
19000,
19356,
19499,
19500,
19995,
19999,
20000,
20248,
20498,
20499,
20500,
20830,
20999,
21000,
21006,
21499,
21500,
21579,
21999,
22000,
22472,
22499,
22500,
22839,
22999,
23000,
23041,
23498,
23499,
23500,
23649,
23999,
24000,
24242,
24499,
24500,
24817,
24999,
25000,
25006,
25498,
25499,
25500,
25676,
25931,
25999,
26000,
26001,
26499,
26500,
26697,
26943,
26999,
27000,
27001,
27491,
27499,
27500,
27504,
27968,
27999,
28000,
28077,
28357,
28499,
28500,
28592,
28970,
28999,
29000,
29126,
29336,
29499,
29500,
29599,
29912,
29999,
30000,
30310,
30499,
30500,
30696,
30950,
30999,
31000,
31062,
31499,
31500,
31563,
31876,
31999,
32000,
32001,
32230,
32499,
32500,
32523,
32840,
32999,
33000,
33139,
33334,
33499,
33500,
33702,
33999,
34000,
34197,
34354,
34499,
34500,
34562,
34894,
34999,
35000,
35350,
35499,
35500,
35546,
35830,
35999,
36000,
36303,
36499,
36500,
36654,
36788,
36999,
37000,
37498,
37499,
37500,
37778,
37953,
37999,
38000,
38054,
38234,
38499,
38500,
38703,
38990,
38999,
39000,
39105,
39290,
39499,
39500,
39691,
39956,
39999,
40000,
40113,
40498,
40499,
40500,
40600,
40999,
41000,
41044,
41301,
41499,
41500,
41503,
41999,
42000,
42018,
42498,
42499,
42500,
42587,
42957,
42999,
43000,
43041,
43493,
43499,
43500,
43596,
43981,
43999,
44000,
44062,
44245,
44499,
44500,
44612,
44758,
44999,
45000,
45063,
45328,
45499,
45500,
45750,
45891,
45999,
46000,
46187,
46379,
46499,
46500,
46581,
46939,
46999,
47000,
47001,
47412,
47499,
47500,
47561,
47763,
47999,
48000,
48185,
48356,
48499,
48500,
48501,
48763,
48999,
49000,
49211,
49498,
49499,
49500,
49549,
49667,
49999,
50000,
50103,
50416,
50499,
50500,
50554,
50820,
50999,
51000,
51002,
51359,
51499,
51500,
51606,
51991,
51999,
52000,
52097,
52498,
52499,
52500,
52560,
52853,
52999,
53000,
53075,
53228,
53499,
53500,
53528,
53928,
53999,
54000,
54115,
54498,
54499,
54500,
54508,
54883,
54999,
55000,
55111,
55499,
55500,
55710,
55913,
55999,
56000,
56169,
56497,
56499,
56500,
56839,
56996,
56999,
57000,
57056,
57498,
57499,
57500,
57628,
57931,
57999,
58000,
58001,
58498,
58499,
58500,
58771,
58999,
59000,
59215,
59499,
59500,
59527,
59847,
59999,
60000,
60016,
60438,
60499,
60500,
60689,
60882,
60999,
61000,
61116,
61498,
61499,
61500,
61736,
61988,
61999,
62000,
62280,
62420,
62499,
62500,
62589,
62998,
62999,
63000,
63011,
63183,
63499,
63500,
63618,
63906,
63999,
64000,
64033,
64321,
64499,
64500,
64514,
64832,
64999,
65000,
65223,
65488,
65499,
65500,
65501,
65997,
65999,
66000,
66328,
66498,
66499,
66500,
66793,
66964,
66999,
67000,
67211,
67413,
67499,
67500,
67760,
67993,
67999,
68000,
68029,
68358,
68499,
68500,
68769,
68998,
68999,
69000,
69195,
69386,
69499,
69500,
69804,
69983,
69999,
70000,
70013,
70225,
70499,
70500,
70706,
70996,
70999,
71000,
71301,
71498,
71499,
71500,
71502,
71999,
72000,
72463,
72499,
72500,
72646,
72999,
73000,
73002,
73217,
73499,
73500,
73996,
73999,
74000,
74022,
74330,
74499,
74500,
74506,
74974,
74999,
75000,
75007,
75483,
75499,
75500,
75506,
75999,
76000,
76006,
76418,
76499,
76500,
76534,
76958,
76999,
77000,
77043,
77495,
77499,
77500,
77524,
77955,
77999,
78000,
78007,
78402,
78499,
78500,
78671,
78992,
78999,
79000,
79076,
79472,
79499,
79500,
79504,
79868,
79999,
80000,
80107,
80496,
80499,
80500,
80805,
80997,
80999,
81000,
81092,
81266,
81499,
81500,
81604,
81998,
81999,
82000,
82021,
82184,
82499,
82500,
82959,
82999,
83000,
83066,
83499,
83500,
83629,
83924,
83999,
84000,
84456,
84499,
84500,
84831,
84999,
85000,
85001,
85484,
85499,
85500,
85501,
85967,
85999,
86000,
86018,
86435,
86499,
86500,
86563,
86725,
86999,
87000,
87117,
87498,
87499,
87500,
87579,
87984,
87999,
88000,
88498,
88499,
88500,
88528,
88932,
88999,
89000,
89018,
89408,
89499,
89500,
89815,
89999,
90000,
90213,
90497,
90499,
90500,
90504,
90688,
90999,
91000,
91454,
91499,
91500,
91882,
91999,
92000,
92003,
92499,
92500,
92574,
92990,
92999,
93000,
93054,
93439,
93499,
93500,
93839,
93999,
94000,
94103,
94495,
94499,
94500,
94856,
94999,
95000,
95116,
95309,
95499,
95500,
95586,
95787,
95999,
96000,
96131,
96495,
96499,
96500,
96936,
96997,
96999,
97000,
97002,
97460,
97499,
97500,
97556,
97901,
97999,
98000,
98068,
98163,
98499,
98500,
98674,
98838,
98999,
99000,
99039,
99408,
99499,
99500,
99602,
99855,
99999,
100000,
100161,
100439,
100499,
100500,
100603,
100974,
100999,
101000,
101145,
101422,
101499,
101500,
101950,
101999,
102000,
102177,
102471,
102499,
102500,
102524,
102999,
103000,
103034,
103152,
103499,
103500,
103511,
103850,
103999,
104000,
104199,
104406,
104499,
104500,
104991,
104999,
105000,
105033,
105192,
105499,
105500,
105618,
105715,
105999,
106000,
106378,
106462,
106499,
106500,
106931,
106999,
107000,
107156,
107494,
107499,
107500,
107525,
107929,
107999,
108000,
108123,
108345,
108499,
108500,
108675,
108999,
109000,
109006,
109179,
109499,
109500,
109708,
109792,
109999,
110000,
110236,
110444,
110499,
110500,
110897,
110999,
111000,
111352,
111498,
111499,
111500,
111697,
111999,
112000,
112100,
112311,
112499,
112500,
112501,
112668,
112999,
113000,
113129,
113439,
113499,
113500,
113501,
113976,
113999,
114000,
114268,
114499,
114500,
114943,
114999,
115000,
115331,
115498,
115499,
115500,
115775,
115989,
115999,
116000,
116024,
116384,
116499,
116500,
116533,
116968,
116999,
117000,
117042,
117455,
117499,
117500,
117623,
117902,
117999,
118000,
118001,
118442,
118499,
118500,
118598,
118999,
119000,
119145,
119328,
119499,
119500,
119997,
119999,
120000,
120309,
120499,
120500,
120595,
120768,
120999,
121000,
121006,
121136,
121499,
121500,
121583,
121768,
121999,
122000,
122289,
122491,
122499,
122500,
122998,
122999,
123000,
123001,
123208,
123499,
123500,
123593,
123999,
124000,
124013,
124498,
124499,
124500,
124535,
124934,
124999,
125000,
125050,
125496,
125499,
125500,
125768,
125994,
125999,
126000,
126034,
126354,
126499,
126500,
126516,
126751,
126999,
127000,
127001,
127313,
127499,
127500,
127760,
127836,
127999,
128000,
128499,
128500,
128824,
128978,
128999,
129000,
129035,
129313,
129499,
129500,
129531,
129975,
129999,
130000,
130282,
130499,
130500,
130945,
130999,
131000,
131030,
131499,
131500,
131732,
131840,
131999,
132000,
132228,
132499,
132500,
132550,
132998,
132999,
133000,
133086,
133245,
133499,
133500,
133524,
133992,
133999,
134000,
134209,
134409,
134499,
134500,
134501,
134735,
134999,
135000,
135323,
135499,
135500,
135816,
135999,
136000,
136024,
136499,
136500,
136572,
136833,
136999,
137000,
137001,
137477,
137499,
137500,
137657,
137909,
137999,
138000,
138018,
138499,
138500,
138995,
138999,
139000,
139498,
139499,
139500,
139995,
139999,
140000,
140001,
140489,
140499,
140500,
140510,
140921,
140999,
141000,
141014,
141445,
141499,
141500,
141502,
141978,
141999,
142000,
142001,
142305,
142499,
142500,
142578,
142867,
142999,
143000,
143074,
143480,
143499,
143500,
143566,
143707,
143999,
144000,
144059,
144491,
144499,
144500,
144501,
144701,
144999,
145000,
145001,
145429,
145499,
145500,
145717,
145997,
145999,
146000,
146018,
146496,
146499,
146500,
146517,
146921,
146999,
147000,
147041,
147477,
147499,
147500,
147515,
147842,
147999,
148000,
148130,
148368,
148499,
148500,
148518,
148794,
148999,
149000,
149190,
149474,
149499,
149500,
149515,
149815,
149999,
150000,
150202,
150499,
150500,
150502,
150752,
150999,
151000,
151018,
151154,
151499,
151500,
151544,
151730,
151999,
152000,
152243,
152422,
152499,
152500,
152779,
152872,
152999,
153000,
153012,
153491,
153499,
153500,
153511,
153958,
153999,
154000,
154059,
154451,
154499,
154500,
154579,
154999,
155000,
155225,
155499,
155500,
155998,
155999,
156000,
156499,
156500,
156501,
156632,
156999,
157000,
157355,
157499,
157500,
157738,
157891,
157999,
158000,
158133,
158339,
158499,
158500,
158502,
158992,
158999,
159000,
159299,
159499,
159500,
159996,
159999,
160000,
160099,
160477,
160499,
160500,
160620,
160997,
160999,
161000,
161085,
161490,
161499,
161500,
161501,
161993,
161999,
162000,
162002,
162415,
162499,
162500,
162503,
162989,
162999,
163000,
163016,
163203,
163499,
163500,
163501,
163986,
163999,
164000,
164022,
164399,
164499,
164500,
164679,
164855,
164999,
165000,
165010,
165085,
165499,
165500,
165506,
165994,
165999,
166000,
166002,
166499,
166500,
166671,
166976,
166999,
167000,
167198,
167484,
167499,
167500,
167686,
167999,
168000,
168010,
168499,
168500,
168710,
168970,
168999,
169000,
169495,
169499,
169500,
169585,
169998,
169999,
170000,
170153,
170469,
170499,
170500,
170779,
170999,
171000,
171152,
171493,
171499,
171500,
171584,
171823,
171999,
172000,
172248,
172499,
172500,
172501,
172999,
173000,
173048,
173386,
173499,
173500,
173889,
173999,
174000,
174034,
174492,
174499,
174500,
174501,
174981,
174999,
175000,
175049,
175475,
175499,
175500,
175999,
176000,
176202,
176498,
176499,
176500,
176557,
176869,
176999,
177000,
177307,
177499,
177500,
177839,
177999,
178000,
178070,
178499,
178500,
178593,
178847,
178999,
179000,
179031,
179499,
179500,
179622,
179860,
179999,
180000,
180133,
180494,
180499,
180500,
180738,
180999,
181000,
181118,
181499,
181500,
181732,
181992,
181999,
182000,
182007,
182439,
182499,
182500,
182501,
182999,
183000,
183107,
183499,
183500,
183597,
183731,
183999,
184000,
184287,
184499,
184500,
184750,
184998,
184999,
185000,
185079,
185340,
185499,
185500,
185710,
185938,
185999,
186000,
186001,
186451,
186499,
186500,
186838,
186999,
187000,
187006,
187499,
187500,
187798,
187934,
187999,
188000,
188499,
188500,
188730,
188999,
189000,
189416,
189499,
189500,
189765,
189997,
189999,
190000,
190132,
190499,
190500,
190520,
190999,
191000,
191127,
191499,
191500,
191887,
191999,
192000,
192117,
192499,
192500,
192565,
192997,
192999,
193000,
193153,
193499,
193500,
193999,
194000,
194489,
194499,
194500,
194501,
194998,
194999,
195000,
195240,
195499,
195500,
195602,
195999,
196000,
196103,
196425,
196499,
196500,
196996,
196999,
197000,
197001,
197196,
197499,
197500,
197854,
197999,
198000,
198174,
198396,
198499,
198500,
198674,
198991,
198999,
199000,
199106,
199377,
199499,
199500,
199792,
199999,
200000,
200001,
200366,
200499,
200500,
200659,
200807,
200999,
201000,
201009,
201268,
201499,
201500,
201678,
201999,
202000,
202063,
202253,
202499,
202500,
202550,
202772,
202999,
203000,
203044,
203426,
203499,
203500,
203502,
203805,
203999,
204000,
204004,
204441,
204499,
204500,
204600,
204832,
204999,
205000,
205190,
205499,
205500,
205742,
205999,
206000,
206388,
206499,
206500,
206683,
206998,
206999,
207000,
207049,
207203,
207499,
207500,
207502,
207958,
207999,
208000,
208002,
208404,
208499,
208500,
208502,
208816,
208999,
209000,
209329,
209499,
209500,
209742,
209921,
209999,
210000,
210191,
210499,
210500,
210846,
210999,
211000,
211294,
211499,
211500,
211518,
211888,
211999,
212000,
212001,
212384,
212499,
212500,
212939,
212999,
213000,
213001,
213499,
213500,
213522,
213747,
213999,
214000,
214001,
214206,
214499,
214500,
214572,
214957,
214999,
215000,
215315,
215404,
215499,
215500,
215637,
215999,
216000,
216062,
216496,
216499,
216500,
216601,
216888,
216999,
217000,
217498,
217499,
217500,
217513,
217817,
217999,
218000,
218164,
218499,
218500,
218996,
218999,
219000,
219005,
219491,
219499,
219500,
219562,
219962,
219999,
220000,
220006,
220487,
220499,
220500,
220502,
220900,
220999,
221000,
221272,
221499,
221500,
221501,
221895]

My python setup: Python 3.9.2, Plotly 5.22.0, kaleido 0.1.0.post1

Hi @shivamk ,

I have updated the code by using your data.

At first, as you said, the bar plot will disappeared.

Then I try to play around with the marker_line_width and width attrtibutes.

So after trying several times here the result:

  1. If you set less value of marker_line_width does not make any significant effect in static image export.
  2. But by setting bigger width attribute, in this case I set width=50, furtunately the bar visible more visible when you export to static image.

here the part of codes that should be updated:

data.append(
    go.Bar(x=x, y=x, marker_color="rgba(255,0,0,1.)",
        visible=True, # Set bar visibility
        width=50., # set bigger values make bar more visible
        name='exh_1_t', legendgroup="exh_1_t(c)",
        marker_line_width=0.009,
        showlegend=True))

Hi @farispriadi,
I also think width is the only way to make bars visible in static image.
But when data is following series like x = [ 0, 1, 2, 3, 4, 5 ] the bar get touches each other.
I want a dynamic code so that it can adjust bar width on its own.

Hi @shivamk ,

I think it is possible to do.
You need to define each of every x data’s width (by setting the width attribute a list of bar width)

You can create function that can identify the interval between current value and both previous and next values.

Calculate and compare with delta previous and next values .
Get the less value and multiply with minimum width min_width.

After that, call the function in list comprehension to get list of bar width and assigned to width attribute.


...

def get_width(val_index,val_list,min_width=0.9):
    # create function that can identify 
    # the interval between current value and both previous and next values.
    # then calculate and compare with delta previous and next values and 
    # get the less value and multiply with minimum width `min_width`
    if val_index >=1 and val_index < len(val_list)-1:
        delta_prev = abs((val_list[val_index-1] - val_list[val_index]))
        delta_next = abs((val_list[val_index+1] - val_list[val_index]))
        if delta_next > delta_prev:
            return delta_prev*min_width
        else:
            return delta_next*min_width
    elif val_index ==  len(val_list)-1: # if the last index
        return abs((val_list[val_index-1] - val_list[val_index])*min_width)
    else: # if val_index == 0:
        return abs((val_list[val_index] - val_list[val_index +1])*min_width)


data = []
data.append(
    go.Bar(x=x, y=x, marker_color="rgba(255,0,0,1.)",
        visible=True, # Set bar visibility
        width= [ get_width(idx,x) for idx, i in enumerate(x)], # call the function in list comprehension to get lis of bar width
        name='exh_1_t', legendgroup="exh_1_t(c)",
        marker_line_width=0.009,
        showlegend=True))


...

Hope this help you to get the closest solution!