1

First of all,

I'm learning to use Python and sometimes it's a little tricky to me.

I'm using a Game of Thrones database from kraggle to learn visualizations. Now I'm trying to see how many character of each hause died in each book.

In this image you can see the dataset how it looks.

Then I make this code:

houses_deathbybook = data_deathsB.groupby(['Book_of_Death', 'Allegiances']).count()[['Name']]

To see a count of deads by house and book.

Then I pivoted the table, with this look

And used the subplot command to achieve this graph.

I'm now trying to make that graph more usefull using this code

fig, axes = plt.subplots(nrows=1, ncols=1, gridspec_kw={'wspace': 0.1, 'hspace': 0.9}) 
data_deathsB.loc[data_deathsB['Allegiances']=='House Arryn'.groupby(['Book_of_Death']).agg('count').plot(x='Book of Death', y='Muertes',kind='bar',figsize=(20,15),color='limegreen',grid=True,ax=axes[1,0], title='House Arryn',fontsize=13)

The second part of the code will go replicate for each house.

But it seems to do not work. I make a test, putting in the grid settings just 1 row and column to check one house, and it gives me the next error "unexpected EOF while parsing".

Could you help me?

3
  • 1
    you forgot a ] in the second line Commented Mar 3, 2019 at 16:50
  • 1
    It will be much easier to spot these kinds of mistakes if you limit the length of your lines. You don't have to write all the functions and their parameters in one line but can use line breaks (usually after 80 characters) or define new variables as an input for your function. Commented Mar 3, 2019 at 16:55
  • Hi! thanks for your comments! I'll take Gregor's comment as a good practice, and I think I found the missing ] The new code is the next one: fig, axes = plt.subplots(nrows=2, ncols=1, gridspec_kw={'hspace': 1}) data_deathsB.loc[data_deathsB['Allegiances']=='House Arryn'].groupby(['Book_of_Death'],as_index=False).plot(x='Book of Death', y='Muertes',kind='bar',figsize=(20,15),color='limegreen',grid=True,ax=axes[0,0],title='House Arryn') When I put another house, it gives me "IndexError: too many indices for array" error.... Commented Mar 3, 2019 at 17:35

2 Answers 2

1

The problem in your second approach is that you have defined a figure with 2 subfigures: having a single column and two rows. So when you have either a single row or a single column, you can't use two indices [0,0] and so on to access the subplots. In this case you will have to use like the following

ax=axes[0],title='House Arryn')

and

ax=axes[1],title='House Arryn')

The two index style [0,0], [0,1] etc. will work when you will have more than one row and one column.

Sign up to request clarification or add additional context in comments.

Comments

0

It worked!

This is the result of the next code (just one of the graphs

fig, axes = plt.subplots(nrows=2,gridspec_kw={'hspace': 1}) data_deathsB.loc[data_deathsB['Allegiances']=='House Arryn',['Allegiances', 'Name', 'Book_of_Death']].groupby(['Book_of_Death'],as_index=False).agg('count').plot(x='Book_of_Death', kind='bar',figsize=(20,15),color='limegreen',grid=True,ax=axes[0],title='House Arryn') data_deathsB.loc[data_deathsB['Allegiances']=='House Baratheon',['Allegiances', 'Name', 'Book_of_Death']].groupby(['Book_of_Death'],as_index=False).agg('count').plot(x='Book_of_Death', kind='bar',figsize=(20,15),color='limegreen',grid=True,ax=axes1,title='House Baratheon')

The next steps would be to make the graphs a little more cute.

Thanks to everyone!

1 Comment

Quick tip: this is probably the worst way to plot data. The code here is nearly unreadable and very obfuscated. I would suggest you familiarize yourself with the matplotlib itself and use its native commands to enhance readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.