【发布时间】:2021-09-17 09:55:08
【问题描述】:
我正在开发以下函数:extract_name_value(),它在 Python 中生成一个采用 pandas DataFrame 值的步骤图,现在它工作正常,但我想将变量 points_axisyvalue 或 values_list 的值添加到它在每个标记中:Script Here
我尝试使用以下示例:Data value at each marker、Matplotlib scatter plot with different text at each data point 或 How to put individual tags for a matplotlib scatter plot?,这将是我想要的;我什至尝试使用plt.annotate(),但值的数据并没有按照我想要的方式出现,而且我认为它会掩盖很多图表并且不能很好地理解。下面我放了我正在使用 plt.annotate() 的代码:
# Function to extract the Name and Value attributes
def extract_name_value(signals_df, rootXML):
# print(signals_df)
names_list = [name for name in signals_df['Name'].unique()]
num_names_list = len(names_list)
num_axisx = len(signals_df["Name"])
values_list = [value for pos, value in enumerate(signals_df["Value"])]
print(values_list)
points_axisy = signals_df["Value"]
print(len(points_axisy))
colors = ['b', 'g', 'r', 'c', 'm', 'y']
# Creation Graphic
fig, ax = plt.subplots(nrows=num_names_list, figsize=(20, 30), sharex=True)
plt.suptitle(f'File XML: {rootXML}', fontsize=16, fontweight='bold', color='SteelBlue', position=(0.75, 0.95))
plt.xticks(np.arange(-1, num_axisx), color='SteelBlue', fontweight='bold')
labels = ['value: {0}'.format(j) for j in values_list]
print(labels)
i = 1
for pos, name in enumerate(names_list):
# get data
data = signals_df[signals_df["Name"] == name]["Value"]
print(data)
# get color
j = random.randint(0, len(colors) - 1)
# get plots by index = pos
x = np.hstack([-1, data.index.values, len(signals_df) - 1])
y = np.hstack([0, data.values, data.iloc[-1]])
ax[pos].plot(x, y, drawstyle='steps-post', marker='o', color=colors[j], linewidth=3)
ax[pos].set_ylabel(name, fontsize=8, fontweight='bold', color='SteelBlue', rotation=30, labelpad=35)
ax[pos].yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
ax[pos].yaxis.set_tick_params(labelsize=6)
ax[pos].grid(alpha=0.4)
i += 1
for label, x, y in zip(labels, x, y):
plt.annotate(label, xy=(x, y), xytext=(-20, 20), textcoords='offset points', ha='right', va='bottom', bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'))
plt.show()
我得到的是在不同位置拼接的注解。
但是,我的代码需要在每个点显示每个值吗?
我也一直在尝试使用 Matplotlib 参考中的代码,但无法完成:Marker Reference。非常感谢您,任何评论都会有所帮助。
【问题讨论】:
-
你能举个例子说明你希望你的地块是什么样子吗?
-
你好@Yulia,理想的是我在onenotes中的例子,我放在这里:example_freehand.png。我认为我们将不得不使用标记为黄色的变量之一或我在下图中显示的最后一个 (y):data_values_chart.png 但我无法找到如何做到这一点。非常感谢。
标签: python python-3.x matplotlib marker subplot