简体   繁体   中英

how to set value for x and y axis while plotting in python

I want to plot a graph like this with multiple lines

https://www.datasciencemadesimple.com/wp-content/uploads/2017/08/Line-chart-in-python-2.png

I have multiple csv files

file1.csv

  1. round sales
  2. 1 19
  3. 2 21
  4. 3 24
  5. 4 56
  6. 5 39

file2.csv

  1. round sales
  2. 1 21
  3. 2 31
  4. 3 41
  5. 4 21
  6. 5 23

i tried but i am only able to merge it to one cvs file

df1 = pd.read_csv("z:/python/1.csv")
df2 = pd.read_csv("z:/python/2.csv")
df2 = df2.dropna(axis=1)
merged = df1.merge(df2, on='sno')
merged.to_csv("output.csv", index=False)

but i am trying to plot through multiple csv files instead of merging first

import matplotlib.pyplot as plt
import pandas as pd
df1 = pd.read_csv("z:/python/file1.csv")
df2 = pd.read_csv("z:/python/file2.csv")
df3 = pd.read_csv("z:/python/file3.csv")
df4 = pd.read_csv("z:/python/file4.csv")
ax = df1.plot(label='df1')
df2.plot(ax=ax)
plt.show()

this is plotting 4 lines on graph.. i only need to plot sales for every round.. i dont want to plot line for round在此处输入图像描述 can anyone help

try accessing the variable df with the key ['sales'], the read_csv() function returns a dict of all columns, try the following:

df1 = pd.read_csv("z:/python/file1.csv")['sales']

replace the following line in your code:

ax = df1.plot(label='df1')

with the following:

ax = df1.plot(label='year-xxxx', x="round", y="sales")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM