Python 打印多行时,希望每行都有一个带有文本标签的标记
我目前正在制作一个图表,其中有4条线穿过它。我想在上面有一堆标记,这样人们就可以在图表上的那个点和时间得到一个特定的值 附件是我的例子。我有一堆不同值的线。我想将它设置为每隔30天,它会在该点的值线上放置一个标记。这样人们可以更容易地判断价值 我的数据集示例Python 打印多行时,希望每行都有一个带有文本标签的标记,python,pandas,matplotlib,Python,Pandas,Matplotlib,我目前正在制作一个图表,其中有4条线穿过它。我想在上面有一堆标记,这样人们就可以在图表上的那个点和时间得到一个特定的值 附件是我的例子。我有一堆不同值的线。我想将它设置为每隔30天,它会在该点的值线上放置一个标记。这样人们可以更容易地判断价值 我的数据集示例 25 50 90 100 2019-04-04 55.0 76.0 1027.0 1200.0 2019-04-05 56.0 77.0 1028.0 1201.0
25 50 90 100
2019-04-04 55.0 76.0 1027.0 1200.0
2019-04-05 56.0 77.0 1028.0 1201.0
2019-04-06 57.0 78.0 1029.0 1202.0
2019-04-07 58.0 79.0 1030.0 1203.0
2019-04-08 59.0 80.0 1031.0 1204.0
2019-04-09 60.0 81.0 1032.0 1205.0
2019-04-10 61.0 82.0 1033.0 1206.0
2019-04-11 62.0 83.0 1034.0 1207.0
2019-04-12 53.0 84.0 1035.0 1208.0
2019-04-13 54.0 85.0 1036.0 1209.0
2019-04-14 55.0 86.0 1037.0 1210.0
2019-04-15 56.0 87.0 1038.0 1211.0
2019-04-16 57.0 88.0 1039.0 1212.0
2019-04-17 58.0 89.0 1040.0 1213.0
2019-04-18 59.0 90.0 1041.0 1214.0
2019-04-19 60.0 91.0 1042.0 1215.0
2019-04-20 61.0 92.0 1043.0 1216.0
2019-04-21 62.0 93.0 1044.0 1217.0
2019-04-22 63.0 94.0 1045.0 1218.0
2019-04-23 64.0 95.0 1046.0 1219.0
2019-04-24 65.0 96.0 1047.0 1220.0
2019-04-25 66.0 97.0 1048.0 1221.0
2019-04-26 67.0 98.0 1049.0 1222.0
2019-04-27 68.0 99.0 1050.0 1223.0
2019-04-28 69.0 100.0 1051.0 1224.0
2019-04-29 70.0 101.0 1052.0 1225.0
2019-04-30 71.0 102.0 1053.0 1226.0
2019-05-01 72.0 103.0 1054.0 1227.0
2019-05-02 73.0 104.0 1055.0 1228.0
2019-05-03 74.0 105.0 1056.0 1229.0
还有我用来绘图的代码
plt.rcParams['figure.figsize'] = [18, 10]
df = pd.DataFrame(data=panda_data)
fig, ax = plt.subplots()
ax = df.plot(kind='line')
ax.grid(axis='y')
对于每日数据,使用
resample.asfreq()
每X天获取一次积分(这里我将使用15作为给定日期范围的说明)。然后绘制它们并注释。确保标签不重叠是一项困难得多的任务
关于几点,不要太担心stack
+iteritems
的性能
import matplotlib.pyplot as plt
import pandas as pd
#df.index = pd.to_datetime(df.index) # If not a DatetimeIndex
pts = df.resample('15D').asfreq()
fig, ax = plt.subplots(figsize=(10, 6))
df.plot(kind='line', ax=ax)
pts.plot(marker='o', ax=ax, lw=0, color='black', legend=False)
pad = 10
for idx, val in pts.stack().iteritems():
ax.annotate(val, (idx[0], val+pad))
ax.grid(axis='y')
当我尝试复制您的pts=part时,我得到以下错误。TypeError:仅对DatetimeIndex、TimedeltaIndex或PeriodIndex有效,但获得了'Index'@Mallachar do
df.Index=pd.to_datetime(df.Index)