Related Articles

# How to make Custom Buttons in Plotly?

• Last Updated : 01 Oct, 2020

A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library.

## Making Custom Buttons

In plotly, actions custom Buttons are used to quickly make actions directly from a record. Custom Buttons can be added to page layouts in CRM, Marketing, and Custom Apps. There are 4 possible methods that can be applied in custom buttons:

• restyle: modify data or data attributes
• relayout: modify layout attributes
• update: modify data and layout attributes
• animate: start or pause an animation

Example 1: Using Restyle Method

## Python3

 `import` `plotly.graph_objects as px``import` `numpy as np`` ` ` ` `# creating random data through randomint``# function of numpy.random``np.random.seed(``42``)`` ` `random_x ``=` `np.random.randint(``1``, ``101``, ``100``)``random_y ``=` `np.random.randint(``1``, ``101``, ``100``)`` ` `plot ``=` `px.Figure(data``=``[px.Scatter(``    ``x``=``random_x,``    ``y``=``random_y,``    ``mode``=``'markers'``,)``])`` ` `# Add dropdown``plot.update_layout(``    ``updatemenus``=``[``        ``dict``(``            ``type``=``"buttons"``,``            ``direction``=``"left"``,``            ``buttons``=``list``([``                ``dict``(``                    ``args``=``[``"type"``, ``"scatter"``],``                    ``label``=``"Scatter Plot"``,``                    ``method``=``"restyle"``                ``),``                ``dict``(``                    ``args``=``[``"type"``, ``"bar"``],``                    ``label``=``"Bar Chart"``,``                    ``method``=``"restyle"``                ``)``            ``]),``        ``),``    ``]``)`` ` `plot.show()`

Output: Example 2: Using Update method

## Python3

 `import` `plotly.graph_objects as px``import` `numpy`` ` ` ` `# creating random data through randomint``# function of numpy.random``np.random.seed(``42``)`` ` `random_x ``=` `np.random.randint(``1``, ``101``, ``100``)``random_y ``=` `np.random.randint(``1``, ``101``, ``100``)`` ` `x ``=` `[``'A'``, ``'B'``, ``'C'``, ``'D'``]`` ` `plot ``=` `px.Figure(data``=``[go.Bar(``    ``name``=``'Data 1'``,``    ``x``=``x,``    ``y``=``[``100``, ``200``, ``500``, ``673``]``),``    ``go.Bar(``    ``name``=``'Data 2'``,``    ``x``=``x,``    ``y``=``[``56``, ``123``, ``982``, ``213``]``)``])`` ` ` ` `# Add dropdown``plot.update_layout(``    ``updatemenus``=``[``        ``dict``(``            ``type``=``"buttons"``,``            ``direction``=``"left"``,``            ``buttons``=``list``([``                ``dict``(label``=``"Both"``,``                     ``method``=``"update"``,``                     ``args``=``[{``"visible"``: [``True``, ``True``]},``                           ``{``"title"``: ``"Both"``}]),``                ``dict``(label``=``"Data 1"``,``                     ``method``=``"update"``,``                     ``args``=``[{``"visible"``: [``True``, ``False``]},``                           ``{``"title"``: ``"Data 1"``,``                            ``}]),``                ``dict``(label``=``"Data 2"``,``                     ``method``=``"update"``,``                     ``args``=``[{``"visible"``: [``False``, ``True``]},``                           ``{``"title"``: ``"Data 2"``,``                            ``}]),``            ``]),``        ``)``    ``])`` ` `plot.show()`

Output: Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.5722966,"math_prob":0.8845773,"size":2686,"snap":"2021-31-2021-39","text_gpt3_token_len":791,"char_repetition_ratio":0.10663684,"word_repetition_ratio":0.28772637,"special_character_ratio":0.33730453,"punctuation_ratio":0.23434344,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9899484,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,7,null,7,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-16T21:09:46Z\",\"WARC-Record-ID\":\"<urn:uuid:b990994b-a30b-4572-b5b9-4ad88768fefb>\",\"Content-Length\":\"121774\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f0b785a3-2a03-460d-9043-85cdaee34b16>\",\"WARC-Concurrent-To\":\"<urn:uuid:d80a8889-4e6d-4c15-92fc-b125fac3bf20>\",\"WARC-IP-Address\":\"23.40.207.73\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/how-to-make-custom-buttons-in-plotly/?ref=lbp\",\"WARC-Payload-Digest\":\"sha1:TBYQOWLBUBTGD7KTXRPF7FQQ5FAJO33H\",\"WARC-Block-Digest\":\"sha1:MKEYPF4RKPIR6TQPFNC4JNEGZLWBL2J6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780053759.24_warc_CC-MAIN-20210916204111-20210916234111-00317.warc.gz\"}"}