简体   繁体   English

如何在熊猫图中为每条线绘制水平线?

[英]How to draw horizontal lines for each line in pandas plot?

I need to draw a horizontal line from the first point in each line drawn below. 我需要从下面绘制的每条线的第一个点开始绘制一条水平线。 The line spans from first xticks to last x-ticks. 该行从第一个xticks到最后一个x-ticks。 To use ax.hlines() , I need to know the xmax and and xmin which I do not know if the x-values are categorical. 要使用ax.hlines() ,我需要知道xmaxxmin ,我不知道x值是否是分类值。 How to draw these horizontal lines (The lines given in dotted pink lines.)? 如何绘制这些水平线(虚线为虚线)。

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

在此处输入图片说明

You do not need to loop over the columns. 您无需遍历各列。 You can simply add the below line to plot the lines. 您可以简单地添加以下行以绘制线条。

All the y parameters for the lines would be the starting points. 线的所有y参数都是起点。 mydf.iloc[0] , xmin would always be ax.get_lim()[0] and xmax would be ax.get_lim()[1] . mydf.iloc[0]xmin始终是ax.get_lim()[0]xmax始终是ax.get_lim()[1]

See docs for more information. 请参阅文档以获取更多信息。

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

Output: 输出:

在此处输入图片说明

Use: 采用:

mydf = pd.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()])

for i in range(len(mydf.columns)):
    ax.axhline(y=mydf.iloc[0,i], xmin=.05, xmax=.95, ls='--', color='pink')

where 哪里

xmin : scalar, optional, default: 0 Should be between 0 and 1, 0 being the far left of the plot, 1 the far right of the plot. xmin:标量,可选,默认值:0应该在0到1之间,0是绘图的最左侧,1是绘图的最右侧。

xmax : scalar, optional, default: 1 Should be between 0 and 1, 0 being the far left of the plot, 1 the far right of the plot. xmax:标量,可选,默认值:1应该在0到1之间,0是绘图的最左侧,1是绘图的最右侧。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM