Sharyn
Sharyn

Reputation: 11

NaN values on my bar chart x axis, removing with .notna() is creating empty space along axis

I am trying to create a bar chart representing positive and negative percentage values. I don't have any empty values in my CSV file:

enter image description here

When I write the following code I get an output with several nan values at the tail end of my bar chart:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.cm as cm

#import
piverror = pd.read_csv("/Users/sharonmandipe/Downloads/Width_APE_histogram_data2.csv")

#filter data to remove distance from RB as a variable on the x axis
piverrorfilt = piverror.drop('Distance from bank (%)', axis=1)


#choose colours
colour = ['darkgrey', 'thistle', 'dimgrey', 'lightsteelblue']

#make a bar chart
ax = piverrorfilt.plot.bar(color = colour)

# Set the x-axis tick labels to the categories (in this case that is oh, of and ns using non-filtered data so we know distance from rb)
plt.xticks(range(len(piverror)), piverror['Distance from bank (%)'])

plt.yscale('linear')

#horizontal line at y=0
plt.axhline(0, color='black', linewidth=1, linestyle='--')

# Adding labels and title
plt.xlabel('Distance from bank (%)')
plt.ylabel('Mean percentage error from reference velocity (%)')

# Set the bar widths
bar_width = 0.2

# Change fill pattern of each bar based on the group category
for i, bar in enumerate(ax.containers[0]):
    bar.set_hatch('xxx')
for i, bar in enumerate(ax.containers[1]):
    bar.set_hatch('///')
for i, bar in enumerate(ax.containers[2]):
    bar.set_hatch('...')


plt.legend()

plt.show()

enter image description here

I have tried to remove the NaN values using the following code:

#drop na values from error variable
piverrornonan= piverror[piverror['Distance from bank (%)'].notna()]

The NaN values have gone, but for some reason the area that they used to occupy on the x axis remains:

enter image description here

How might I optimise my bar chart to not include this gap at the tail end? Also, why would these NaN values be coming up if I don't have any NaN values in my original csv? Thanks

[Edit to add data in text format]: Distance from bank (%) Scenario 1 Scenario 2 Scenario 3 0 - 25

-7 6 25 - 50 2 3

50 - 75 8

8 75 - 100

6

Upvotes: 0

Views: 45

Answers (0)

Related Questions