简体   繁体   English

带有自定义刻度的散点图

[英]Scatter plot with custom ticks

I want to do a scatter plot of a wavelength (float) in y-axis and spectral class (list of character/string) in x-axis, labels = ['B','A','F','G','K','M']. 我想做一个y轴波长(浮点)的散点图和x轴的光谱类(字符/字符串列表),标签= ['B','A','F','G' , 'K', 'M'。 Data are saved in pandas dataframe, df. 数据保存在pandas dataframe,df中。

df['Spec Type Index']
0      NaN
1        A
2        G
.        .
.        .
167      K
168    Nan
169      G

Then, 然后,

df['Disk Major Axis "']
0        4.30
1        4.50
2       22.00
.         .
.         .
167      1.32
168      0.28
169     25.00

Thus, I thought this should be done simply with 因此,我认为这应该简单地用

plt.scatter(df['Spec Type Index'], df['Disk Major Axis "'])

But I get this annoying error 但我得到了这个恼人的错误

could not convert string to float: 'G' 无法将字符串转换为浮点数:'G'

After fixing this, I want to make custom xticks as follows. 修好后,我想制作如下自定义xticks。 However, how can I 但是,我怎么能

labels = ['B','A','F','G','K','M']
ticks = np.arange(len(labels))
plt.xticks(ticks, labels)

First, I think you have to map those strings to integers then matplotlib can decide where to place those points. 首先,我认为你必须将这些字符串映射到整数,然后matplotlib可以决定放置这些点的位置。

labels = ['B','A','F','G','K','M']
mapping = {'B': 0,'A': 1,'F': 2,'G': 3,'K': 4,'M': 5}
df = df.replace({'Spec Type Index': mapping})

Then plot the scatter, 然后绘制散点图,

fig, ax = plt.subplots()
ax.scatter(df['Spec Type Index'], df['Disk Major Axis "'])

Finally, 最后,

ax.set_xticklabels(labels)

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

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