首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用python制作非数值型x轴直方图

用python制作非数值型x轴直方图
EN

Stack Overflow用户
提问于 2018-07-23 08:54:40
回答 1查看 296关注 0票数 1

我有一个快速/肮脏的脚本,它将质量分类为光谱类型,并制作一个质量直方图,如下所示

代码语言:javascript
复制
import matplotlib.pyplot as plt
import numpy as np
% matplotlib inline

star_masses = np.array([0.359, 0.325,0.842, 0.359, 0.245, 0.445, 0.558, 0.117, 0.245, 0.177, 0.558, 0.058, 0.637, 0.222, 0.245, 0.039, 0.177])
M2 = []
M12 = []
K7 = []
M6 = []
M1 = []
M0 = []
M5 = []
M35 = []
M42 = []
M625 = []
M37 = []
T6 = []
M8 = []

for i in star_masses:
    if i == 0.359:
        M2.append(i)
    if i == 0.325:
        M12.append(i)
    if i == 0.842:
        K7.append(i)
    if i == 0.245:
        M6.append(i)
    if i == 0.445:
        M1.append(i)
    if i == 0.558 or 0.637:
        M0.append(i)
    if i == 0.117:
        M5.append(i)
    if i == 0.245:
        M35.append(i)
    if i == 0.058:
        M625.append(i)
    if i == 0.222:
        M37.append(i)
    if i == 0.039:
        T6.append(i)
    if i == 0.177:
        M8.append(i)

plt.hist(star_masses, bins = 15)
plt.xlabel('Stellar Mass')
plt.ylabel('# of Stars')
plt.title ('Stellar Mass')

我正在尝试制作光谱类型的直方图,以便每个bin都是x轴上的光谱类型的名称,y轴是恒星的数量,但我不知道如何在x轴上获得非数值的值

EN

回答 1

Stack Overflow用户

发布于 2018-07-23 09:17:44

您可以使用set_xticklabels

代码语言:javascript
复制
import matplotlib.pyplot as plt
import numpy as np

star_masses = np.array([0.359, 0.325,0.842, 0.359, 0.245, 0.445, 0.558, 0.117, 0.245, 0.177, 0.558, 0.058, 0.637, 0.222, 0.245, 0.039, 0.177])
star_names= ['star%i'%ii for ii in range(13)]
M2 = []
M12 = []
K7 = []
M6 = []
M1 = []
M0 = []
M5 = []
M35 = []
M42 = []
M625 = []
M37 = []
T6 = []
M8 = []

for i in star_masses:
    if i == 0.359:
        M2.append(i)
    if i == 0.325:
        M12.append(i)
    if i == 0.842:
        K7.append(i)
    if i == 0.245:
        M6.append(i)
    if i == 0.445:
        M1.append(i)
    if i == 0.558 or 0.637:
        M0.append(i)
    if i == 0.117:
        M5.append(i)
    if i == 0.245:
        M35.append(i)
    if i == 0.058:
        M625.append(i)
    if i == 0.222:
        M37.append(i)
    if i == 0.039:
        T6.append(i)
    if i == 0.177:
        M8.append(i)

f, ax = plt.subplots()
ax.hist(star_masses, bins = 15)
ax.set_xlabel('Stellar Mass')
ax.set_xticklabels(star_names)
ax.set_ylabel('# of Stars')
ax.set_title ('Stellar Mass')
plt.show()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51470408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档