Holoviews - How to create side-by-side bars from dataframe columns?

pongo30

I would like to create a Holoviews bar chart (using Bokeh backend) in which Year is on the X-axis and columns A and B on the Y-Axis. For each Year, I want bars for values from columns A and B to appear next to each other i.e. for Year 2008, I have bars of heights 1 and 3, for year 2009, I have bars 3 and 6 height, and so on. I have tried numerous different ways including the example of grouped bars in the documentation but can't get it to work. See example below:

%%opts Bars [xrotation=90 width=600 show_legend=False tools=['hover']]
df=pd.DataFrame({'Year':[2008,2009,2010,2011,2012,2013],
   'A': [1,2,3,4,5,6],'B':[3,6,9,12,15,18]})
print(df)
bars = hv.Bars(df, kdims=['Year'], vdims=['A'])
bars

Please help. I am losing my mind!

philippjfr

HoloViews generally works best when your data is in what's called a tidy format. However to make it easier to work with data like yours we have developed a companion library called hvPlot. To generate the plot you want you can simply run:

import hvplot.pandas
df=pd.DataFrame({'Year':[2008,2009,2010,2011,2012,2013],
   'A': [1,2,3,4,5,6],'B':[3,6,9,12,15,18]})
df.hvplot.bar('Year')

enter image description here

Alternatively you can learn about the pd.melt method, which can take your data in a wide format and convert it to a tidy dataset:

%%opts Bars [xrotation=90 width=600 show_legend=False tools=['hover']]
df=pd.DataFrame({'Year':[2008,2009,2010,2011,2012,2013],
   'A': [1,2,3,4,5,6],'B':[3,6,9,12,15,18]})
tidy_df = df.melt(id_vars=['Year'], value_vars=['A', 'B'])
bars = hv.Bars(tidy_df, ['Year', 'variable'], ['value'])
bars

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to create table from result of client side function \crosstabview

分類Dev

How to position bars along side the X axis in bar3?

分類Dev

How to put 2 video side by side and create chunks of given duration?

分類Dev

RESTful PHP: how does it work from client side to server side?

分類Dev

Bootstrap columns aren't side by side

分類Dev

stuck trying to get these two columns side by side

分類Dev

Placing two columns side by side with image and text

分類Dev

How to join series of file together side by side without seeing any gap between columns in output?

分類Dev

Python: How to create DataFrame columns from a dict inside a list

分類Dev

How to create side by side two video with padding and image background using ffmpeg?

分類Dev

Prevent flex items from rendering side to side

分類Dev

Split code from result side by side in sqldeveloper

分類Dev

HoloViews: create boxplots for every column in a pandas dataframe

分類Dev

How to align ul list side by side?

分類Dev

how can i pass java object from server side to client side

分類Dev

How to Receive JSON sent from client side to server Side in cakePHP 3.x

分類Dev

Bootstrap 3: Cant make two affix side bars

分類Dev

how to use assertoff from test to disable assertion in side uvm object

分類Dev

(Server Side) How to retrieve data from MarkLogic and paginate a few at a time

分類Dev

How do I recover from an expired Kuzzle token server side?

分類Dev

Python 3: How to log SSL handshake errors from server side

分類Dev

How to update a server side datatable on change a checkbox from outside of datatable

分類Dev

How to append data to the repeater control from client side

分類Dev

How to use substr from right hand side to left hand side? How to convert each 8 characters from a string to an int?

分類Dev

User confirmation from server side

分類Dev

How to create a dictionary with two dataframe columns in pyspark?

分類Dev

How to prevent scroll for side navbar?

分類Dev

How to create rating bars in Android?

分類Dev

How can i put 3 divs side by side with a margin and a border?

Related 関連記事

  1. 1

    How to create table from result of client side function \crosstabview

  2. 2

    How to position bars along side the X axis in bar3?

  3. 3

    How to put 2 video side by side and create chunks of given duration?

  4. 4

    RESTful PHP: how does it work from client side to server side?

  5. 5

    Bootstrap columns aren't side by side

  6. 6

    stuck trying to get these two columns side by side

  7. 7

    Placing two columns side by side with image and text

  8. 8

    How to join series of file together side by side without seeing any gap between columns in output?

  9. 9

    Python: How to create DataFrame columns from a dict inside a list

  10. 10

    How to create side by side two video with padding and image background using ffmpeg?

  11. 11

    Prevent flex items from rendering side to side

  12. 12

    Split code from result side by side in sqldeveloper

  13. 13

    HoloViews: create boxplots for every column in a pandas dataframe

  14. 14

    How to align ul list side by side?

  15. 15

    how can i pass java object from server side to client side

  16. 16

    How to Receive JSON sent from client side to server Side in cakePHP 3.x

  17. 17

    Bootstrap 3: Cant make two affix side bars

  18. 18

    how to use assertoff from test to disable assertion in side uvm object

  19. 19

    (Server Side) How to retrieve data from MarkLogic and paginate a few at a time

  20. 20

    How do I recover from an expired Kuzzle token server side?

  21. 21

    Python 3: How to log SSL handshake errors from server side

  22. 22

    How to update a server side datatable on change a checkbox from outside of datatable

  23. 23

    How to append data to the repeater control from client side

  24. 24

    How to use substr from right hand side to left hand side? How to convert each 8 characters from a string to an int?

  25. 25

    User confirmation from server side

  26. 26

    How to create a dictionary with two dataframe columns in pyspark?

  27. 27

    How to prevent scroll for side navbar?

  28. 28

    How to create rating bars in Android?

  29. 29

    How can i put 3 divs side by side with a margin and a border?

ホットタグ

アーカイブ