Home > Mobile >  Trying to remove names from pyplot bar graph
Trying to remove names from pyplot bar graph

Time:02-03

So I am working with some data for a science fair project, and I am extremely new to pandas and matplotlib/pyplot. I am currently trying to make a graph of some data (a bar graph) and have been able to do so fine. I split my DataFrame into two parts: the name and the values themselves:

data = pd.read_csv('results.csv')
data = data.sort_values(by=['Accuracy'], ascending=False)
accuracy = data['Accuracy']
names = data['Name']

This works fine. And when I go to make my graph it also works fine:

plt.bar(names, accuracy)
plt.title('Accuracy Below 97%')
plt.ylabel('Accuracy in Percent')
plt.show()

But the only problem is that when I do this, my names are too long so it ends up as a sort of blur: enter image description here

I also have around 40 data points which I understand is probably too many to be able see the names anyways, but the names are around 30 characters long so even if I reduced the amount of data points in a graph, it still would probably not work.

So I then I just assumed that I would remove names from plt.bar(names, accuracy) but this throws the error:

TypeError: bar() missing 1 required positional argument: 'height'

So I realized that I need a width value, and since the number of data point was 42 I then tried:

plt.bar(42, accuracy)

But this creates a weird graph that I am not looking for: enter image description here

So my question is: how do I remove the names from the graph while keeping the actual graph the same?

Any help is greatly appreciated. Thanks!

CodePudding user response:

Here is what you want but you can handle those with this link instead of deleting the problem.

datetime x-axis matplotlib labels causing uncontrolled overlap

ax = data[['Accuracy','Name']].plot(title='Accuracy Below 97%')
ax.get_xaxis().set_visible(False)
pyplot.show()

CodePudding user response:

if you want to remove the xticks labels from the graph you can do

plt.xticks([])

Also, you can adjust the x-axis limits to remove the labels completely.

plt.xticks([])
plt.xlim(-0.5, len(accuracy)-0.5)
  • Related