-1

I would to plot corresponding values on line charts. Can you please let me know how can I do that?

import matplotlib.pyplot as plt
x = [1,2,3]
y = [3,4,5]
z = [6,7,8]

plt.plot(x, y)
plt.plot(x, z)
plt.show()

Thanks.

Add
  • 407
  • 1
  • 4
  • 9
  • I am able to plot correctly. However, I would like to have value on line chart for which I am looking out for a syntax. – Add Feb 07 '22 at 08:21
  • What is the value of a line chart? The x-y line has, for instance, six values, three each for x and y. Which value do you want to add to the chart? Or do you want to add [labels and a legend](https://matplotlib.org/stable/tutorials/introductory/usage.html#parts-of-a-figure)? I think you should start by reading [the tutorial](https://matplotlib.org/stable/tutorials/introductory/pyplot.html) – Mr. T Feb 07 '22 at 08:27
  • I have gone through the tutorial, but I would like to have y value along the line for the first line and the z value for the second line. – Add Feb 07 '22 at 08:33
  • This has been explained, for instance, here: https://stackoverflow.com/q/46063077/8881141 – Mr. T Feb 07 '22 at 08:36
  • I have gone through the link, but in my case I have two lines in the same chart. If I follow the same approach it disturb the y axis. Could you please help me to solve above query with code. – Add Feb 07 '22 at 10:12
  • What is the specific code you tried to implement and what does it mean that is "disturbs the y-axis"? Please include the code, the current output, and a description of the difference to the expected output. – Mr. T Feb 07 '22 at 10:16
  • I tried but still it doesn't produce correct output. In case of y axis although both values are more or less similar but it produce lines which are far apart from each other. I request if you could help to code above so that I can see if it work for me. – Add Feb 07 '22 at 11:39

1 Answers1

1

You can add value tag to the point by using a for loop over the data points in cartesian coordinate.

import matplotlib.pyplot as plt
x = [1,2,3]
y = [3,4,5]
z = [6,7,8]

plt.plot(x, y, '*--')
for a,b in zip(x, y): 
    plt.text(a, b, str(b),fontsize=20)
plt.plot(x, z, '*--')
for a,b in zip(x, z): 
    plt.text(a, b, str(b),fontsize=20)

plt.ylim(0, 10)
plt.show()

enter image description here

Ka Wa Yip
  • 2,546
  • 3
  • 22
  • 35