Python Forum

Full Version: please check this i wanna use a csv file as a graph
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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')
i is a str which must match one of the column names. Use df[i], not df.i
(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
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.
(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
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()