0

How can I display the data(y axis values) on the matlotlib graph? I found this related post, I tried suggested solution but it doesn't work here, at least I couldn't figure it out. I'd appreciate if you can help. thanks.

What I try to do here is to plot the y values (mean,aver,dist) against time in three different plots.

Here is my code:

import matplotlib.pyplot  as plt
import numpy as np
import pandas as pd
import mysql.connector
from matplotlib import style
from matplotlib.pyplot import figure
from matplotlib import pyplot

conn = mysql.connector.MySQLConnection(user='root', password='', host='127.0.0.1', database='DB')
cursor = conn.cursor()
sql = "SELECT Time, mean, Aver, Dist FROM datatb order by Time Desc limit 100"
cursor.execute(sql)
result = cursor.fetchall()

df = pd.DataFrame(list(result),columns=["Time","Mean","Aver","Dist"])


plt.figure(num=None, figsize=(15, 12), dpi=150, facecolor='w', edgecolor='k')

plt.subplot(2,1,1)
plt.title('Mean')
plt.plot(df['Time'], df["Mean"],'o-', c='blue',label=r'$Mean$')
plt.legend(loc='best')
ax = plt.gca()
ax.invert_xaxis()

plt.figure(num=None, figsize=(15, 12), dpi=150, facecolor='w', edgecolor='k')
plt.subplot(2,1,2)
plt.title('Aver')
plt.plot(df['Time'], df["Aver"],'o-', c='green', label=r'$Aver$')
plt.legend(loc='best')
ax = plt.gca()
ax.invert_xaxis()

plt.figure(num=None, figsize=(15, 12), dpi=150, facecolor='w', edgecolor='k')
plt.subplot(2,1,2)
plt.title('Dist')
plt.plot(df['Time'], df["Dist"], 'o-', c='brown', label=r'$Dist$')
plt.legend(loc='best')
ax = plt.gca()
ax.invert_xaxis()


conn.close()
cursor.close()

My plot looks like this:

enter image description here

seralouk
  • 30,938
  • 9
  • 118
  • 133
user223549
  • 117
  • 8
  • I am not sure, what your question is - your subplots seem to be ... unusual. [Maybe this is your aim?](https://matplotlib.org/gallery/subplots_axes_and_figures/shared_axis_demo.html#sphx-glr-gallery-subplots-axes-and-figures-shared-axis-demo-py) – Mr. T Oct 26 '18 at 18:48
  • Where is the part of the code where you tried the linked solution? In what way did it not work? – Diziet Asahi Oct 26 '18 at 18:48
  • can you add the data ? – seralouk Oct 26 '18 at 18:54
  • What I try to do is to display the y-values on the line dots. – user223549 Oct 26 '18 at 19:06
  • I tried this part, `for i,j in zip(x,y): ax.annotate(str(j),xy=(i,j))` but I did't know how to implement it exactly, and it didn't work either. – user223549 Oct 26 '18 at 19:07
  • check this https://matplotlib.org/api/_as_gen/matplotlib.pyplot.annotate.html – seralouk Oct 26 '18 at 19:08

1 Answers1

2

Use annotate:


Example:

import numpy
from matplotlib import pyplot

x = numpy.arange(10)
y = numpy.array([5,3,4,2,7,5,4,6,3,2])

fig = pyplot.figure()
ax = fig.add_subplot(111)
ax.set_ylim(0,10)
pyplot.plot(x,y)
for i,j in zip(x,y):
    ax.annotate(str(j),xy=(i,j))

pyplot.show()

Now, just replace x and y with df['Time'] and df["Mean"].

enter image description here

seralouk
  • 30,938
  • 9
  • 118
  • 133