0

Ok, I'm sufficiently confused and not making any progress at all. New to python, and using Pycharm/python 3

I have a text file saved on my desktop (Win 10). The file contains a name, a comma and a number.

e.g.

name1, number

name2, number

name3, number

name4, number

(etc)

The names are all different, and the numbers are not ordered, and some are the same and some are not.

The numbers are integers, and all I'm trying to do is make a bar chart plot. The names along the x-axis and the numbers (in descending order) for the y-axis.

I have tried and tried and tried and tried and I cannot get this to work.

I can manage to access the file, but can't do anything with it. I can get the output I want, but I can't access it. I can get bits and pieces but I can't put it together. I know this is probably an easy thing, but nothing makes sense, and nothing seems to work anymore, and could use a hand.

How do you do this?

Thank you

2 Answers 2

1

I think this would be the simplest implementation:

#import libraries 
import pandas as pd 
import matplotlib.pyplot as plt 
#read your txt file which is formatted as a csv into a dataframe and name your cols
df = pd.read_csv('my_file.txt',names=['name','number'])
print(df.head())

#plot it
plt.bar(df.name,df.number) #this is equivalent to df['name'],df['number']
plt.show()

There are a lot of other ways to make this more complicated, improve your plot ensure your datatypes are correct etc. but this will hopefully get you going.

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

14 Comments

Thanks brobertsp, but this throws a number of errors that I probably have no hope of fixing.
You changed the 'my_file.txt' to whatever the filepath you have is? As long as you did that and your file is formatted as name, number each on its own line this should work. You can always post your errors on here as well for further explanation of your problem.
brobertsp --- yes, I did make that change, and I know it works because I accessed the file and did a print and it DOES show, so I believe I am accessing it correctly. Thanks for all your help....errors...that's just it, there are NO errors.....it says: "Process finished with exit code 0"...and it throws up a plot window with axes, numbers, tick marks....and no plot. Now....I did find a code snippet for bar charts and histograms and I can copy that in and run it, and that gives a nice chart....works great, but for some reason it doesn't like my text file I guess.....it's pretty frustrating, lol
brobertsp ---- I can run the following and it accesses and prints the contents of a text file on my desktop. file1 = open(r"C:\Users\name\Desktop\my_file_name.txt", "r") print("Output of Read function is ") print(file1.read())
brobertsp --- Ok...I'm so confused, BUT....I changed the path to a different text file, ran the program (same result), changed it back to the main text file....and now it's working. It prints a nice chart....names along the bottom, sorted.....it's beautiful, I'm grateful to you, but I don't understand why all this happened this way. I'm 99% sure it was something I was doing, but I don't know what....thank you, the only thing I need to do it print the names along the x-axis so they are at an angle so they all fit and can be read. I appreciate your help.
|
1

Something like this will work. Let me know if you have any questions.

import matplotlib.pyplot as plt

filepath = r"C:\Users*me*\Desktop\my_file.txt"
with open(filepath) as file:
    entries = [x.split(",") for x in file.readlines()] # Read the text, splitting on comma.
    entries = [(x[0],int(x[1])) for x in entries] # Turn the numbers into ints.
    entries.sort(key=lambda x:x[1], reverse=True) # Sort by y-values.
    x_coords = [x[0] for x in entries]
    y_coords = [x[1] for x in entries]
    plt.bar(x_coords,y_coords) # Draw a bar chart
    plt.show()

4 Comments

Thank you....there's no errors, but the plot is empty. It's probably on my end, and not accessing the text file correctly. How do I access the file on my desktop. I use: C:\Users*me*\Desktop\my_file.txt
Sure, I've fixed it to how you would load a filepath.
Thanks Jimmy, yes, I did do the file path right, and the code runs with no errors, but the plot window is blank.
Should "filename" be changed to "filepath"? Even if so, I tried making the change and still get a blank chart. It plots the axes and the tick marks are there, but everything else is blank.

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.