0

Good day! I am making a django app that would plot graphs from a text document. In the text document data is aligned this way:

name 10
name_2 20
name_3 100
typeofthegrah

I just split everything by spaces and put in 2 lists (names and values), then I just do plt.bar(names, values) and get something like this graphs

name1 189
name2 149 
name3 23
name4 82
name5 245
name6 90
column

I tried sorting them with zip but it didn't work. It works perfectly if I plot it as a pie chart but linear and bar graphs are broken.

heyper_2
  • 41
  • 3

2 Answers2

2

The values to plot are being read in as strings which is why the y-axis is broken. Try casting the values as int or float by using something like:

values = [float(x) for x in values]
Tharmee
  • 131
  • 2
  • Thank you very much! I just implemented a function that checks if some string can be converted to float but I didn't actually make a conversion. – heyper_2 May 26 '21 at 14:43
-1

Here's Your Solution:

import matplotlib.pyplot as plt
import numpy as np

x = np.array(["name1", "name2", "name3", "name4", "name5", "name6"])
y = np.array([189, 149, 23, 82, 245, 90])

plt.bar(x,y)
plt.show()
Kanishk Mewal
  • 399
  • 2
  • 10