Python Forum
please check this i wanna use a csv file as a graph
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please check this i wanna use a csv file as a graph
#1
PLEASE just read this and help me
so i made this program where i input any csv file that could be imported in the form of a valid dataframe and , assuming the user knows the names of the 2 x and y axis columns i wrote the code as the following
before you check the code , the error is that i used a variable input "i" and let the user input the name of the x axis into it and so in the plot command line , it says "i" is not a dataframe funtion ? why ? i already let the user input "i" as the valid xaxis name from the dataframe
well if my method is wrong then can anyone please suggest another method ? pleaseeee

import pandas as pd
import matplotlib.pyplot as pl
b=input('enter the pathway of your file')
df=pd.read_csv(b)
print(df)
def bar() :
    i=input('enter the name of the x axis')
    l=input('enter the name of the y axis')
    df.plot.bar(df.i,df.l)
    pl.title('record of computer department')
    pl.show()
def line() :
    v=input('enter the name of the x axis')
    z=input('enter the name of the y axis')
    df.plot(df.v,df.z,marker='*')
    pl.title('record of computer department')
    pl.show()
print('enter 1 for bar graph')
print('enter 2 for line graph')
x=int(input("CHOOSE YOUR GRAPH OF CHOICE"))

if x==1:
      bar()
elif x==2:
      line()
else:
      print('enter a valid number')
Yoriz write Aug-24-2022, 06:03 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
i is a str which must match one of the column names. Use df[i], not df.i
Reply
#3
(Aug-24-2022, 07:23 PM)deanhystad Wrote: i is a str which must match one of the column names. Use df[i], not df.i

now my first problem of "'i" not being a dataframe function is solved
but now i get this error
[Image: unknown.png]
this is my file if you want to see whats wrong
https://media.discordapp.net/attachments...nknown.png
thanks for helping with the first error.
even better if u help me with the second and hopefully the last
Reply
#4
Looking at it a bit more I see that the plot commands are wrong. This is how you plot a bar chart using the plotting commands in pandas:
def bar(df):
    x = input("enter the name of the x axis ")
    y = input("enter the name of the y axis ")
    df.plot.bar(x=x, y=y)
    pl.title("record of computer department")
    pl.show()
And this is for a line plot.
def line(df):
    x = input("enter the name of the x axis ")
    y = input("enter the name of the y axis ")
    df.plot.line(x=x, y=y)
    pl.title("record of computer department")
    pl.show()
Things are different if you plot from matplotlib, and without looking closely, this is how I answered your question. Using matplotlib, this is how you plot a bar graph using data from a dataframe.
def bar(df):
    x = input("enter the name of the x axis ")
    y = input("enter the name of the y axis ")
    pl.bar(df[x], df[y])
    pl.title("record of computer department")
    pl.show()
and this is how you would plot a line graph
def line(df):
    x = input("enter the name of the x axis ")
    y = input("enter the name of the y axis ")
    pl.plot(df[x], df[y])
    pl.title("record of computer department")
    pl.show()
As a complete aside, NEVER EVER EVER use "l" as a variable name. Same goes for "o". In many fonts it is impossible to differentiate these from one and zero. I used "x" and "y" since they were being used to represent the x axis and y axis data.
Reply
#5
(Aug-24-2022, 08:21 PM)deanhystad Wrote: Looking at it a bit more I see that the plot commands are wrong. This is how you plot a bar chart using the plotting commands in pandas:
def bar(df):
    x = input("enter the name of the x axis ")
    y = input("enter the name of the y axis ")
    df.plot.bar(x=x, y=y)
    pl.title("record of computer department")
    pl.show()
And this is for a line plot.
def line(df):
    x = input("enter the name of the x axis ")
    y = input("enter the name of the y axis ")
    df.plot.line(x=x, y=y)
    pl.title("record of computer department")
    pl.show()
Things are different if you plot from matplotlib, and without looking closely, this is how I answered your question. Using matplotlib, this is how you plot a bar graph using data from a dataframe.
def bar(df):
    x = input("enter the name of the x axis ")
    y = input("enter the name of the y axis ")
    pl.bar(df[x], df[y])
    pl.title("record of computer department")
    pl.show()
and this is how you would plot a line graph
def line(df):
    x = input("enter the name of the x axis ")
    y = input("enter the name of the y axis ")
    pl.plot(df[x], df[y])
    pl.title("record of computer department")
    pl.show()
As a complete aside, NEVER EVER EVER use "l" as a variable name. Same goes for "o". In many fonts it is impossible to differentiate these from one and zero. I used "x" and "y" since they were being used to represent the x axis and y axis data.

omg it finally worked, thanks a lot for your help !! Big Grin
Reply
#6
Reducing some of the code duplication and making it easy to support more plot types..
import pandas as pd
import matplotlib.pyplot as plt

plots = {
    "Bar": "bar",
    "Line": "line",
    "Scatter": "scatter",
}

df = pd.read_csv(input("CSV file to plot: "))
func = getattr(df.plot, plots[input(f"Plot Type {list(plots)} ")])
print(f"Select signals to plot {list(df)}")
func(input("X: "), input("Y: "))
plt.title("record of computer department")
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  check if a file exist on the internet and get the size kucingkembar 6 1,758 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,929 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Check last time file was accessed Pavel_47 4 2,817 Jun-01-2021, 05:47 PM
Last Post: Yoriz
  How to check if a file has finished being written leocsmith 2 7,815 Apr-14-2021, 04:21 PM
Last Post: perfringo
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,943 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Check if a file exists. Pedroski55 5 3,292 Sep-08-2020, 10:01 AM
Last Post: Pedroski55
  How to check to see a dbf file is EOF ? DarkCoder2020 0 1,719 Jun-16-2020, 05:03 PM
Last Post: DarkCoder2020
  Building a script to check size of file upon creation mightyn00b 2 2,382 Apr-04-2020, 04:39 AM
Last Post: Larz60+
  MySql - Loop - CHeck File gcclinux 1 2,083 Nov-30-2019, 11:51 AM
Last Post: ThomasL
  Getting the maximum point of the graph from the text file Erfans 2 2,601 Mar-09-2019, 11:01 PM
Last Post: Erfans

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020