0

I need to create a plot with different timeseries.

I have a text.txt like this:

its     0.9961912799250892      0.9611789138018554      0.9362671060056597      0.8763435242194505      1.0123654188938198      1.0049162314011486      0.9288035817512186  0.9084643027881887      1.0359986661523464      0.9292172475731887      1.1323221737681437      0.9417832856370081      1.1047062436329529 1.041934255132639        0.8649039179711691      1.0204522265992209      1.0302435960614755      0.9439750410654008      1.088471553700333       1.09443521050185    1.094738841363382       0.8871657870692601
have    0.9803308671900406      0.9319386476562331      1.0331349519024478      0.867925915500978       0.9418640951068216      0.9581122075343849      1.0434859486538306  0.9784010340794705      1.025276431179925       0.894842387816677       1.0426014897136142      0.9170958122168531      0.9487792151977866 0.9964108286346515       0.9793456479955277      1.1058475490104895      1.0554666275922342      0.9567847468398236      0.9998158381848763      0.8830721222359678  0.9137870179095002      0.9944767442737839
under   0.9521242806442476      0.8767673648366237      1.0239817655007064      0.9396657835488991      1.0076041959612225      0.9237786710498844      0.9521946479254487  0.9956320162468837      0.8814118029892077      1.032370688033565       0.9669434386215183      1.122806639381777       1.025567084981407  1.0062144304292062       0.9926387598136613      0.9547875216868695      0.9618588437927801      1.0455172602020864      0.9473457277768003      0.8911914030515552  1.0176010770127335      0.9631461830111424
this    1.0131703508477727      0.9369051361704035      0.8256745555878671      0.9894340912221133      1.0382011195134848      0.97076049287372        0.9417728563760344  0.9163234629257282      1.0186611562591992      1.0171331253531573      0.9941285856866834      1.0718598130635215      0.96655507887686   1.005748397975273        0.9134092919323901      1.0118800412323383      0.8902753397256384      0.8797728402151531      0.9814046832452061      1.032977758970378   1.1082981907514788      1.0007004580638508

How can i create a single plot overlapping these timeseries? The axis are in range(2001,2022).

Of course , i need differents colours to distinguish timeseries and a little legend.

I tried to find some tutorials on this library, but i didn't found nothing about my problem

2
  • It is very easy to make nice plots with seaborn. Seaborn works closely with pandas so I recommend to familiarize yourself with that. You can start with 10 minutes to pandas.
    – elehtine
    Commented Mar 24, 2023 at 11:12
  • There's nothing in the data indicating this is a timeseries, which would have datetime stamps. It's not clear what The axis are in range(2001,2022) is supposed to mean. This question lacks clarity. Commented Mar 24, 2023 at 16:40

2 Answers 2

1

You could do the following (using pandas and seaborn's lineplot):

import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

# load in your text file
data = pd.read_csv(
    "text.txt",
    delimiter="\s+",  # this allow multiple spaces as separators from the data in the file
    header=None,
).T  # the .T transposes the data (i.e., flips the rows and columns)

# get it so that the columns have the correct names
D = data.iloc[1:].copy()
D.columns = data.iloc[0].tolist()

# get dates
dates = pd.date_range(start="1/1/2002", periods=22, freq="Y")

# set DateFrame index to the dates
D.index = dates

# create the plot
fig, ax = plt.subplots()
sns.lineplot(data=D, ax=ax)

fig.show()

This gives the following image:

enter image description here

If you look at the lineplot documentation you can see a lot more customisation that you can do.

0
-1

I copied the data you provided us with on my clipboard and used the following code to plot multiple time series on the same axis. :

import pandas as pd
import seaborn as sns

df = pd.read_clipboard(header = None).T
df.columns = df.loc[0].tolist() # set columns
df.drop(0, inplace = True) # drop first row

for i in df.columns:
    sns.lineplot(x = df.index, y=i, data=df, label = i)

You can achieve similar results using matplotlib.

Please note that on the data you provided us with, we didn't have a column "date", therefore, I used df.index instead.

If you want to change the legend, you can change the option "label = " If you want a specific color, you can use the option color="green".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.