0

I have two time series to plot in single box using matplotlib in python. However, I can see only one line instead of two.

I have a data frame which contain three columns. "A"," B","C". I would like to plot a line chart for "B" and "C" but it only shows "C". Here in my dataset which is pandas data frame and code to plot chart.

LI_DF = pd.DataFrame({'A':[0.01,0.03,0.05,0.1],'B':[-315668.07,-944777.38,-1568942.67,-3066239.14],'C':[-317217.21,-949398.31,-1576561.72,-3080043.6]})
plt.figure(figsize=(16,8), dpi=150))
LI_DF.plot(label='LI_DF', color=['orange','green'])
plt.legend()

I have checked time series type for all three of them and they are same.

Please see attached two screenshot for data frame and final chart output.

Database LI_DF image

Final chart output
enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • Your code is missing a closing `)` – DollarAkshay Mar 20 '22 at 06:26
  • Typo here is mainly while writing code to post a question. However, in original code everything is fine. – Abhay Dodiya Mar 20 '22 at 07:30
  • 2
    Then please do update your question fixing the typos and providing a minimal reproducible example. – DollarAkshay Mar 20 '22 at 07:54
  • There are commas in those numbers, so they are strings. You need to remove all the commas and then convert to float: `LI_DF = LI_DF.replace(',', '', regex=True).astype(float)` – tdy Mar 20 '22 at 08:08
  • @DollarAkshay I have updated the question with an actual small set of data. Can you please check if you can reproduce the line chart for 'B' and 'C' ? – Abhay Dodiya Mar 20 '22 at 11:40

2 Answers2

0
plt.figure(figsize=(16,8), dpi=150))

There was a missing ), strange that there wasn't an error raised.

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
0

The example you provided has a lot of typos. I think you probably copied it wrong because you should be seeing an error and not a chart.

Fixing those errors :

  • Missing closing bracket in pd.DataFrame(columns=['A','B','C']
  • Missing closing single quote in color=['orange','green])

should actually give you 2 line graphs in a single plot.

Code

small_df = pd.DataFrame({ 'A' : [0, 1, 1, 2, 3, 5], 'B' : [1, 2, 3, 4, 5, 6] })
small_df.plot(color=['orange','green'])
plt.show()

Output

enter image description here

DollarAkshay
  • 2,063
  • 1
  • 21
  • 39
  • Typo here is mainly while writing code to post a question. However, in original code everything is fine. I am still not able to replicate the same with my original dataset. Is there anything else apart from highlighted above ? – Abhay Dodiya Mar 20 '22 at 07:31
  • @AbhayDodiya did you try running my example code that I posted here ? – DollarAkshay Mar 20 '22 at 07:53
  • I tried your example and it's working fine. In case of my example, it doesn't work. I believe there is an issue with data may be due to float as your example is of type int. – Abhay Dodiya Mar 20 '22 at 08:22
  • 1
    Can you please provide a [minimal, complete, and reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). You should also read [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – Mr. T Mar 20 '22 at 08:54
  • @Mr.T I have updated the question with an actual small set of data. Can you please check if you can reproduce the line chart for 'B' and 'C' ? – Abhay Dodiya Mar 20 '22 at 11:39
  • 1
    No, three lines, as the legend will tell you. B and C are too similar, though - they appear to be one line. Set one of the B-values to positive to see the difference. `plt.figure(figsize=(16,8), dpi=150)` is also without effect as you don't pass this figure (the axis object associated with it, to be precise) to the following pandas plot. – Mr. T Mar 20 '22 at 11:56
  • 1
    Here is the output of your sample code with one B data point altered for demonstration purposes: https://imgur.com/lR0f2tn – Mr. T Mar 20 '22 at 12:05
  • @Mr.T I am not concerned about "A" as I am more interested in getting B and C. I know both are identical but still, their values are different it should show two lines with thin margins. – Abhay Dodiya Mar 20 '22 at 13:10
  • The differences are below the resolution of your initial image. If you [zoom in](https://imgur.com/RAdbdrE), you see them. – Mr. T Mar 20 '22 at 13:14