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

2022-02-27 00:00:00 python pandas matplotlib plot seaborn

问题描述

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

相关文章