I want to plot the line graph with so many lines. I found the below basic commend to plot 1 line.
import matplotlib.pyplot as plt
location_3='C:\\Users\\Poon\\Downloads\\20211014_SBS_BEMS\\1043 final.csv'
csvfiles_3=glob.glob(location_3)
df7=pd.DataFrame()
for file_new_3 in csvfiles_3:
df7=pd.read_csv(file_new_3,skiprows=[0])
print(df7)
ax=plt.plot(df7.iloc[:, 1],df7.iloc[:, 2],'g')
But I need to plot more than 100 lines. Is there a faster way? Here are some of my data set in excel. There is more column than I show.
19/5/2019 0:00 237 64.93 82.35 16.15 46.88 18.11 148.3115
19/5/2019 1:00 237.2667 64.93 82.35 16.15 46.88 18.11 148.3295
19/5/2019 2:00 238 64.93 82.35 16.15 46.88 18.11 148.348
19/5/2019 3:00 238 64.93 82.35 16.15 46.88 18.11 148.3672
19/5/2019 4:00 238 64.93 82.35 16.15 46.88 18.11 148.4432
19/5/2019 5:00 238 64.93 82.35 16.15 46.88 18.11 148.7437
19/5/2019 6:00 238 64.93 82.35 16.15 46.88 18.11 149.064
19/5/2019 7:00 238 64.93 82.35 16.15 46.88 18.11 149.3825
19/5/2019 8:00 238 64.93 82.35 16.15 46.88 18.11 149.7037
19/5/2019 9:00 238.7833 64.93 82.35 16.15 46.88 18.11 150.0222
19/5/2019 10:00 239 64.93 82.35 16.15 46.88 18.11 150.3422
19/5/2019 11:00 239 64.93 82.35 16.15 46.88 18.11 150.6617
19/5/2019 12:00 239 64.93 82.35 16.15 46.88 18.11 150.9813
19/5/2019 13:00 239 64.93 82.35 16.15 46.88 18.11 151.3018
19/5/2019 14:00 239 64.93 82.35 16.15 46.88 18.11 151.6213
19/5/2019 15:00 239 64.93 82.35 16.15 46.88 18.11 151.94
19/5/2019 16:00 239.4167 64.93 82.35 16.15 46.88 18.11 152.2615
19/5/2019 17:00 240 64.93 82.35 16.15 46.88 18.11 152.5812
19/5/2019 18:00 240 64.93 82.35 16.15 46.88 18.11 152.8295
19/5/2019 19:00 240 64.93 82.35 16.15 46.88 18.11 152.8608
19/5/2019 20:00 240 64.93 82.35 16.15 46.88 18.11 152.8797
19/5/2019 21:00 240 64.93 82.35 16.15 46.88 18.11 152.8962
19/5/2019 22:00 240 64.93 82.35 16.15 46.88 18.11 152.9148
19/5/2019 23:00 240.05 64.93 82.35 16.15 46.88 18.11 152.9337
20/5/2019 0:00 241 64.93 82.35 16.15 46.88 18.11 152.9508
So What I want is the x-axis to be the date, and Y-axis is column 2, then 3,4....until the end.
Moreover, the x-axis needs to reduce the number of plot ticks. Now there are too many plot ticks. How can I reduce them to "only year"? For example, there should be only 2019,2020,2021,2022.
Thanks a lots guys for your help.
CodePudding user response:
Hi and welcome to Stackoverflow. You can use the below code to read the data (I just copied the data you provided and edited it for a couple of years and the data points to increase over time), then create the plots and finally add the timeline in years in the X-axis. The dataframe has Date as header for first date column and the other data columns are named A through G. The legend has some text, but you can edit it as you need.
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates
df=pd.read_excel("inputfile.xlsx")
df['Date'] = pd.to_datetime(df['Date'])
plt.plot(df.Date, df.A, label = "line 1")
plt.plot(df.Date, df.B, label = "line 2")
plt.plot(df.Date, df.C, label = "line 3")
plt.plot(df.Date, df.D, label = "line 4")
plt.plot(df.Date, df.E, label = "line 5")
plt.plot(df.Date, df.F, label = "line 6")
plt.plot(df.Date, df.G, label = "line 7")
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.gca().xaxis.set_major_locator(mdates.YearLocator())
plt.legend()
plt.show()
Output pic
CodePudding user response:
If you want to generate lines automatically, you can use a for cycle leaving out xaxis column like this (added example data for clearance, you can still read from your file):
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
data = {
"date": ['19/5/2019 0:00', '19/5/2019 1:00', '19/5/2020 2:00', '20/5/2021 3:00', '21/5/2022 1:00'],
"colA": [237, 237, 238, 237, 240],
"colB": [64.93, 64.94, 64.95, 65.52, 65.32],
"colC": [644.93, 644.94, 644.95, 654.52, 654.32]
}
df = pd.DataFrame(data)
df.date=pd.to_datetime(df.date)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.gca().xaxis.set_major_locator(mdates.YearLocator())
for col in df.columns:
if not col == 'date':
plt.plot(df.date, df[col], label=col)
plt.legend()
plt.show()
Result:
Extra note: you can add base to YearLocator to change the frequency of years, for example, every 5 years would be:
plt.gca().xaxis.set_major_locator(mdates.YearLocator(base=5))