【问题标题】:xlabel and ylabel values are not sorted in matplotlib scatterplotxlabel 和 ylabel 值未在 matplotlib 散点图中排序
【发布时间】:2018-07-14 17:21:44
【问题描述】:

我在互联网上进行了大量的搜索,但似乎我无法弄清楚如何提出正确的问题来获得我想做的事情的答案。

我正在尝试创建一个散点图,其中 P/E 比率 在 y 轴上,股息收益率 在 x 轴上。我将数据放入 CSV 文件,然后将每一列作为单独的列表导入 Python。

下面是我的散点图。我很困惑为什么 x 轴和 y 轴没有按数字排序。我想我必须将列表中的元素转换为浮点数,然后在 将其转换为散点图之前进行某种排序。

我能想到的另一个选择是能够在创建散点图的过程中对值进行排序。

这些都没有解决,我已经走到了死胡同。任何帮助或指出正确的方向将不胜感激,因为我只能描述我的问题,但似乎无法在我的搜索中提出正确的问题。

import csv
import matplotlib.pyplot as plt

etf_data = csv.reader(open('xlv_xlu_combined_td.csv', 'r'))

for i, row in etf_data.iterrows():
    symbol.append(row[0])
    index.append(row[1])
    dividend.append(row[2])
    pe.append(row[3])

symbol.pop(0)
index.pop(0)
dividend.pop(0)
pe.pop(0)

indexes = [i.split('%', 1)[0] for i in index]
dividend_yield = [d.split('%', 1)[0] for d in dividend]
pe_ratio = [p.split('X', 1)[0] for p in pe]

x = dividend_yield[:5]
y = pe_ratio[:5]

plt.scatter(x, y, label='Healthcare P/E & Dividend', alpha=0.5)
plt.xlabel('Dividend yield')
plt.ylabel('Pe ratio')
plt.legend()
plt.show()

xlv_xlu_combined_td.csv

symbol,index,dividend,pe
JNJ,10.11%,2.81%,263.00X
UNH,7.27%,1.40%,21.93X
PFE,6.48%,3.62%,10.19X
MRK,4.96%,3.06%,104.92X
ABBV,4.43%,4.01%,23.86X
AMGN,3.86%,2.72%,60.93X
MDT,3.50%,2.27%,38.10X
ABT,3.26%,1.78%,231.74X
GILD,2.95%,2.93%,28.69X
BMY,2.72%,2.81%,97.81X
TMO,2.55%,0.32%,36.98X
LLY,2.49%,2.53%,81.83X

【问题讨论】:

    标签: python matplotlib scatter-plot


    【解决方案1】:

    您需要将字符串转换为数字。 Matplotlib 将字符串视为“类别”,并按照您提供它们的顺序绘制它们。

    【讨论】:

      【解决方案2】:
      • 问题在于这些值是string 类型,因此它们是按照列表中给出的顺序而不是数字顺序绘制的。
      • 值必须从末尾删除符号,然后转换为数字类型。

      使用csv 模块添加到现有代码

      • 鉴于现有代码,很容易将列表中的map() 值转换为float 类型。
      indexes = [i.split('%', 1)[0] for i in index]
      dividend_yield = [d.split('%', 1)[0] for d in dividend]
      pe_ratio = [p.split('X', 1)[0] for p in pe]
      
      # add mapping values to floats after removing the symbols from the values
      indexes = list(map(float, indexes))
      dividend_yield = list(map(float, dividend_yield))
      pe_ratio = list(map(float, pe_ratio))
      
      # plot
      x = dividend_yield[:5]
      y = pe_ratio[:5]
      
      plt.scatter(x, y, label='Healthcare P/E & Dividend', alpha=0.5)
      plt.xlabel('Dividend yield')
      plt.ylabel('Pe ratio')
      plt.legend(bbox_to_anchor=(1, 1), loc='upper left')
      plt.show()
      

      使用pandas

      • 从带有col.str[:-1] 的列中的字符串末尾删除符号
      • 使用.astype(float) 将列转换为float 类型
      • 使用pandas v1.2.4matplotlib v3.3.4
      • 此选项将所需代码从 23 行减少到 4 行。
      import pandas as pd
      
      # read the file
      df = pd.read_csv('xlv_xlu_combined_td.csv')
      
      # remove the symbols from the end of the number and set the columns to float type
      df.iloc[:, 1:] = df.iloc[:, 1:].apply(lambda col: col.str[:-1]).astype(float)
      
      # plot the first five rows of the two columns
      ax = df.iloc[:5, 2:].plot(x='dividend', y='pe', kind='scatter', alpha=0.5,
                                ylabel='Dividend yield', xlabel='Pe ratio',
                                label='Healthcare P/E & Dividend')
      ax.legend(bbox_to_anchor=(1, 1), loc='upper left')
      

      绘制两种实现的输出

      • 请注意,数字现在已正确排序。

      【讨论】:

        【解决方案3】:

        我没有足够的代表来回复 cmets 关于 OP 对 Jody 评论的回应,但我想补充一点,这确实为我解决了问题,但如果你遇到与我相同的问题数据框中的多种类型,使用格式只转换一列:

        df["colName"] = pd.to_numeric(df["colName"])

        希望这对某人有所帮助

        【讨论】:

          【解决方案4】:
          import matplotlib.pyplot as plt
          
          #arrays (X,Y) from your csv file with all of your data
          x = [<some values>]
          y = [<some values>]
          
          plt.scatter(X,Y)
          

          这将为您提供一个图,其中每个点的坐标为

          (x[i],y[i])
          

          据我所知,它不会在绘制之前为您自动对数据进行排序。如果你想要排序的数据,你必须首先做一些类似

          的事情
          x.sort()
          y.sort()
          

          然后将它们存储在一个新变量中,然后将其放入 scatter 函数中。

          我看到的另一个问题是,在您的散点图中,X 轴和 Y 轴标签不按顺序排列。我以前从未见过这种情况,我不知道为什么会这样。您能否提供一些代码来诊断为什么会发生这种情况?

          【讨论】:

            猜你喜欢
            • 2020-05-13
            • 2020-11-08
            • 2013-04-15
            • 2014-06-23
            • 1970-01-01
            • 2012-02-04
            • 2021-01-03
            • 2014-06-26
            • 2019-12-23
            相关资源
            最近更新 更多