pandas - 如何为 Pandas 图中的每条线绘制水平线?

标签 pandas plot

我需要从下面绘制的每条线的第一个点开始绘制一条水平线。该线从第一个 xticks 跨越到最后一个 x-ticks。要使用 ax.hlines(),我需要知道 xmaxxmin,我不知道 x 值是否是分类的.如何绘制这些水平线(粉红色虚线给出的线。)?

mydf = DataFrame( [[0.70, 0.79, 0.89, 0.76],
               [0.73, 0.79, 0.80, 0.67],
               [0.74, 0.70, 0.79, 0.87],
               [0.74, 0.60, 0.79, 0.7],
               [0.74, 0.79, 0.79, 0.7]])
mydf.columns = ['f1', 'f2','f3','f4']
mydf.index = ['a','b','c','d','e']
ax = mydf.plot()
ax.set_xticks(range(len(mydf.index)))
ax.set_xticklabels([item for item in mydf.index.tolist()])

enter image description here

最佳答案

您不需要遍历列。您可以简单地添加以下行来绘制线条。

线条的所有 y 参数都是起点。 mydf.iloc[0]xmin 将始终是 ax.get_lim()[0]xmax 将是 ax.get_lim()[1]

参见 docs获取更多信息。

ax.hlines(mydf.iloc[0], ax.get_xticks().min(), ax.get_xticks().max(), linestyle='--', color='pink')

输出:

enter image description here

关于pandas - 如何为 Pandas 图中的每条线绘制水平线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51622120/

相关文章:

python - 如何在seaborn jointgrid/jointplot中注释边际图/分布图

python - Matplotlib-Imported PNG 不会在 3D 绘图中显示

python - 如何在Python中根据多个条件更新列值?

python - 将值直接循环到 csv

R:在同一轴上绘制多条线,平均值以粗体显示?

r - 如何在 ggplot2 中使用用户定义颜色的六边形分箱来绘制点

matlab - 如何在 Matlab 中将 3D 路径数据绘制为带状

python - pandas DataFrame 中的条件操作

python使用排名号来选择两个数据帧之间的值

python - Pandas "if X not in DataFrame"总是抛出错误