python - 绘制多行,每行都需要一个带有文本标签的标记
问题描述
我目前正在制作一个图表,其中有 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
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')
推荐阅读
- javascript - 使用 i18n 的时刻格式
- apache-kafka - KStream copartition join 使用不同模式名称的 avro 键
- c# - 捕获雨水算法复杂度
- linux - 获取安装在数组变量 BASH 中的 yum list
- javascript - 点击加载更多数据直到列表长度
- ios - iOS WebKit 无法显示从库或相机中选择的图像
- logging - 在 log4rs 中检索记录器
- python - 根据产品组的创建和交货日期计算订单队列
- python - struct.unpack() struct.error: unpack 需要 124 字节的缓冲区
- node.js - 基于属性值的 Avj 条件模式验证