• <legend id='sdv0p'><style id='sdv0p'><dir id='sdv0p'><q id='sdv0p'></q></dir></style></legend>

    <small id='sdv0p'></small><noframes id='sdv0p'>

      <bdo id='sdv0p'></bdo><ul id='sdv0p'></ul>

      1. <tfoot id='sdv0p'></tfoot>
        <i id='sdv0p'><tr id='sdv0p'><dt id='sdv0p'><q id='sdv0p'><span id='sdv0p'><b id='sdv0p'><form id='sdv0p'><ins id='sdv0p'></ins><ul id='sdv0p'></ul><sub id='sdv0p'></sub></form><legend id='sdv0p'></legend><bdo id='sdv0p'><pre id='sdv0p'><center id='sdv0p'></center></pre></bdo></b><th id='sdv0p'></th></span></q></dt></tr></i><div id='sdv0p'><tfoot id='sdv0p'></tfoot><dl id='sdv0p'><fieldset id='sdv0p'></fieldset></dl></div>

        将字符串数据传递给matplotlib API时会绘制什么?

        时间:2024-08-20

          <small id='ekPpm'></small><noframes id='ekPpm'>

          <legend id='ekPpm'><style id='ekPpm'><dir id='ekPpm'><q id='ekPpm'></q></dir></style></legend>

              <i id='ekPpm'><tr id='ekPpm'><dt id='ekPpm'><q id='ekPpm'><span id='ekPpm'><b id='ekPpm'><form id='ekPpm'><ins id='ekPpm'></ins><ul id='ekPpm'></ul><sub id='ekPpm'></sub></form><legend id='ekPpm'></legend><bdo id='ekPpm'><pre id='ekPpm'><center id='ekPpm'></center></pre></bdo></b><th id='ekPpm'></th></span></q></dt></tr></i><div id='ekPpm'><tfoot id='ekPpm'></tfoot><dl id='ekPpm'><fieldset id='ekPpm'></fieldset></dl></div>

              • <bdo id='ekPpm'></bdo><ul id='ekPpm'></ul>
                    <tbody id='ekPpm'></tbody>

                • <tfoot id='ekPpm'></tfoot>
                • 本文介绍了将字符串数据传递给matplotlib API时会绘制什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  # first, some imports:
                  import numpy as np
                  import pandas as pd
                  import matplotlib.pyplot as plt
                  

                  假设我要使用以下数据绘制散点图:

                  np.random.seed(42)
                  x=np.arange(0,50)
                  y=np.random.normal(loc=3000,scale=1,size=50)
                  

                  打印方式:

                  plt.scatter(x,y)
                  

                  我得到的答案是:

                  好的,我们先创建一个数据帧:

                  df=pd.DataFrame.from_dict({'x':x,'y':y.astype(str)})
                  

                  (我知道我将y存储为str-这是一个可重现的示例,我这样做是为了反映真实的用例。)

                  那么,如果我这样做了:

                  plt.scatter(df.x,df.y)
                  

                  我得到:


                  我在第二个绘图中看到的是什么?我认为第二个绘图一定是显示x列相对于y列绘制的,它们被转换为浮点型。显然不是这样的。

                  推荐答案

                  • 如果提取标签和位置,API会将字符串绘制为标签,轴位置是基于存在多少(len)类别的0索引号。
                  • .get_xticks().get_yticks()提取数字位置列表。
                  • .get_xticklabels().get_yticklabels()提取matplotlib.text.TextText(x, y, text)的列表。
                  • y轴的列表中的数字较少,因为四舍五入导致存在重复值。
                  • 适用于任何以matplotlib为后台的API,如seabornpandas
                    • sns.scatterplot(data=df, x='x_num', y='y', ax=ax1)
                    • ax1.scatter(data=df, x='x_num', y='y')
                    • ax1.plot('x_num', 'y', 'o', data=df)

                  标签、区域和文本

                  print(x_nums_loc)
                  print(y_nums_loc)
                  print(x_lets_loc)
                  print(y_lets_loc)
                  print(x_lets_labels)
                  
                  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
                  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
                  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
                  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
                  
                  [Text(0, 0, 'A'), Text(1, 0, 'B'), Text(2, 0, 'C'), Text(3, 0, 'D'), Text(4, 0, 'E'),
                   Text(5, 0, 'F'), Text(6, 0, 'G'), Text(7, 0, 'H'), Text(8, 0, 'I'), Text(9, 0, 'J'),
                   Text(10, 0, 'K'), Text(11, 0, 'L'), Text(12, 0, 'M'), Text(13, 0, 'N'), Text(14, 0, 'O'),
                   Text(15, 0, 'P'), Text(16, 0, 'Q'), Text(17, 0, 'R'), Text(18, 0, 'S'), Text(19, 0, 'T'),
                   Text(20, 0, 'U'), Text(21, 0, 'V'), Text(22, 0, 'W'), Text(23, 0, 'X'), Text(24, 0, 'Y'),
                   Text(25, 0, 'Z')]
                  

                  导入、数据和绘图

                  import numpy as np
                  import string
                  import pandas as pd
                  import matplotlib.pyplot as plt
                  import string
                  
                  # sample data
                  np.random.seed(45)
                  x_numbers = np.arange(100, 126)
                  x_letters = list(string.ascii_uppercase)
                  y= np.random.normal(loc=3000, scale=1, size=26).round(2)
                  df = pd.DataFrame.from_dict({'x_num': x_numbers, 'x_let': x_letters, 'y': y}).astype(str)
                  
                  # plot
                  fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 3.5))
                  df.plot(kind='scatter', x='x_num', y='y', ax=ax1, title='X Numbers', rot=90)
                  df.plot(kind='scatter', x='x_let', y='y', ax=ax2, title='X Letters')
                  
                  x_nums_loc = ax1.get_xticks()
                  y_nums_loc = ax1.get_yticks()
                  
                  x_lets_loc = ax2.get_xticks()
                  y_lets_loc = ax2.get_yticks()
                  
                  x_lets_labels = ax2.get_xticklabels()
                  
                  fig.tight_layout()
                  plt.show()
                  

                  这篇关于将字符串数据传递给matplotlib API时会绘制什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:pandas :我如何对堆叠的条形图进行分组? 下一篇:海运HISPLOT和DISPLOT输出不匹配

                  相关文章

                  <i id='acCUR'><tr id='acCUR'><dt id='acCUR'><q id='acCUR'><span id='acCUR'><b id='acCUR'><form id='acCUR'><ins id='acCUR'></ins><ul id='acCUR'></ul><sub id='acCUR'></sub></form><legend id='acCUR'></legend><bdo id='acCUR'><pre id='acCUR'><center id='acCUR'></center></pre></bdo></b><th id='acCUR'></th></span></q></dt></tr></i><div id='acCUR'><tfoot id='acCUR'></tfoot><dl id='acCUR'><fieldset id='acCUR'></fieldset></dl></div>
                  <legend id='acCUR'><style id='acCUR'><dir id='acCUR'><q id='acCUR'></q></dir></style></legend>

                      <small id='acCUR'></small><noframes id='acCUR'>

                      • <bdo id='acCUR'></bdo><ul id='acCUR'></ul>
                      <tfoot id='acCUR'></tfoot>