如何注释/显示p上的y值

2024-09-30 18:32:24 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在matlotlib图形上显示数据(y轴值)? 我找到了这个相关的post,我尝试了建议的解决方案,但它在这里不起作用,至少我想不出来。如果你能帮忙,我将不胜感激。谢谢。你知道吗

这里我试着用三个不同的图来绘制y值(mean,aver,dist)与时间的关系。你知道吗

这是我的密码:

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()

我的情节是这样的:

enter image description here


Tags: fromimportdftimematplotlibdistasplt
1条回答
网友
1楼 · 发布于 2024-09-30 18:32:24

使用annotate


示例:

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()

现在,用df['Time']df["Mean"]替换xy。你知道吗

enter image description here

相关问题 更多 >