简体   繁体   English

如何将熊猫系列绘制为水平线?

[英]How to plot a pandas series as horizontal lines?

I have the following code: 我有以下代码:

import pandas as pd
import matplotlib.pyplot as plt


df = pd.read_csv("data.csv")

short_run = df["High"][df["Volume"] >= df["Volume"].quantile(0.999)][1:22]

short_run.plot()
plt.show()

Output of short_run : short_run输出:

1012     0.000189
1013     0.000167
11696    0.000096
25019    0.000075
25020    0.000076
25071    0.000077
25072    0.000078
25073    0.000081
25079    0.000085
25080    0.000089
25081    0.000090
25083    0.000095
25084    0.000099
25085    0.000117
25086    0.000106
25087    0.000103
25088    0.000100
25089    0.000090
25090    0.000092
25092    0.000089
25093    0.000086

Which outputs the following diagram: 输出如下图:

产量

I was wondering how instead of getting a line plot I could instead get a bunch of horizontal lines (which intersect the y-axis based on the values of short_run ). 我想知道如何代替获得线图而获得一堆水平线(基于short_run的值与y轴short_run )。 In other words how can I turn the previous diagram into this one: 换句话说,我怎样才能将上一张图变成这张图:

期望的输出

(PS: I don't need the red line, I just included to make it easier to understand) (PS:我不需要红线,只是为了使它易于理解而包括在内)

Thank you very much for your help 非常感谢您的帮助

You failed to provide a Minimal, Complete, and Verifiable example . 您没有提供最小,完整和可验证的示例 Since I do not have your DataFrame, I can tell you an indirect solution. 由于我没有您的DataFrame,因此我可以告诉您间接解决方案。

The idea is to get the axis instance returned by the df.plot and then get the y-data. 这个想法是获取df.plot返回的轴实例,然后获取y数据。 Then, loop over the values and plot a horizontal line at each of them. 然后,在这些值上循环并在每个值上绘制一条水平线。 To hide your actual red line, the easiest thing to my mind is to use white color again since you did not provide any reproducible code 要隐藏实际的红线,我想最简单的方法是再次使用白色,因为您没有提供任何可复制的代码

ax = df.plot(color='white')
line = ax.lines[0]
for y in line.get_ydata():
    plt.axhline(y)

Alternatively, assuming your y-values are in a variable y_val , you do not need to first plot the DataFrame. 或者,假设您的y值位于变量y_val ,则无需首先绘制DataFrame。 You can directly use matplotlib as 您可以直接使用matplotlib作为

import matplotlib.pyplot as plt

for y in y_val:
    plt.axhline(y)

plt.show()

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

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