Reputation: 11
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:
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()
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:
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
Upvotes: 0
Views: 45