0

I have some data of the form:

Name Score1 Score2 Score3 Score4 Bob -2 3 5 7

and im trying to use bqplot to plot a really basic bar chart

enter image description here

i'm trying:

sc_ord = OrdinalScale()
y_sc_rf = LinearScale()

bar_chart = Bars(x=data6.Name,
             y=[data6.Score1, data6.Score2, data6.Score3],
             scales={'x': sc_ord, 'y': y_sc_rf},
             labels=['Score1', 'Score2', 'Score3'],
            )

ord_ax = Axis(label='Score', scale=sc_ord, grid_lines='none')
y_ax = Axis(label='Scores', scale=y_sc_rf,  orientation='vertical', 
grid_lines='solid')

Figure(axes=[ord_ax, y_ax],  marks=[bar_chart]) 

but all im getting is one bar, i assume because Name only has one value, is there a way to set the column headers as the x data? or some other way to solve this

  • Happy to try another plotting package too if it makes it easier – Alistair Downes Oct 19 '18 at 11:13
  • X should be same length as y. Is the data in a pandas dataframe? Just change x to data.columns.tolist()[1:]. Or something like that. And give the figure a title of data6.Name. – DougR Oct 27 '18 at 08:41

1 Answers1

0

I think this is what Doug is getting at. Your length of x and y data should be the same. In this case, x is the column labels, and y is the score values. You should set the 'Name' column of your DataFrame as the index; this will prevent it from being plotted as a value.

PS. Next time, if you make sure your code is a complete example that can be run from scratch without external data (a MCVE, https://stackoverflow.com/help/mcve) you are likely to get a much quicker answer.

BQPlot documentation has lots of good examples using the more complex pyplot interface which are worth reading: https://github.com/bloomberg/bqplot/blob/master/examples/Marks/Object%20Model/Bars.ipynb

from bqplot import *
import pandas as pd
data = pd.DataFrame(
    index = ['Bob'],
    columns = ['score1', 'score2', 'score3', 'score4'],
    data = [[-2, 3,5,7]]
)

sc_ord = OrdinalScale()
y_sc_rf = LinearScale()

bar_chart = Bars(x=data.columns, y = data.iloc[0],
             scales={'x': sc_ord, 'y': y_sc_rf},
             labels=data.index[1:].tolist(),
            )

ord_ax = Axis(label='Score', scale=sc_ord, grid_lines='none')
y_ax = Axis(label='Scores', scale=y_sc_rf,  orientation='vertical', 
grid_lines='solid')

Figure(axes=[ord_ax, y_ax],  marks=[bar_chart]) 

enter image description here

ac24
  • 5,325
  • 1
  • 16
  • 31