为 .txt 文件中的数据绘制直方图,名称在 x 轴上,数字在 y 轴上
Plot histogram for data from .txt file with names on x-axis and number on y axis
我是编程新手,我曾尝试绘制值的直方图。这是我第一次尝试在 x 轴上绘制名称直方图。我浏览了这些帖子,但发现 none 很有帮助。我将非常感谢并感谢您的帮助。
数据保存在.txt文件中
数据
Seyfert1 112
Seyfert2 42
Quasars 131
QuasarsSeyfert1 46
Radiogalaxy 5
BLlacHP 39
Seyfert3 5
HeIIRegions 55
使用的代码如下。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
f = open('hist.txt','r')
x,y = np.loadtxt(f,unpack=True, usecols=[0,1])
plt.hist(y, bins=10, histtype='bar',facecolor = 'red', rwidth=1.0)
plt.xlabel('Type of AGN',fontsize=15)
plt.ylabel('Number of galaxies',fontsize=15)
plt.title('Distribution of Type of active galaxies',fontsize=15)
plt.savefig('hist1.pdf',fmt='pdf')
plt.show()
我用同样的方法绘制带有数字的直方图。不确定我哪里出错了。
在 Mr.JohanC 的帮助下,我找到了问题的答案。
这是最后的程序
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
f = open('hist.txt','r')
x, y = np.loadtxt(f, unpack=True, usecols=[0, 1], dtype=str)
y = y.astype(int)
plt.bar(x, y)
plt.tick_params(axis='x', labelrotation=10, labelsize= 10)
plt.xlabel('Type of AGN',fontsize=15)
plt.ylabel('Number of galaxies',fontsize=15)
plt.title('Distribution of Type of active galaxies',fontsize=15)
plt.tight_layout()
plt.savefig('hist_gal.pdf',fmt='pdf')
plt.show()
我是编程新手,我曾尝试绘制值的直方图。这是我第一次尝试在 x 轴上绘制名称直方图。我浏览了这些帖子,但发现 none 很有帮助。我将非常感谢并感谢您的帮助。
数据保存在.txt文件中
数据
Seyfert1 112
Seyfert2 42
Quasars 131
QuasarsSeyfert1 46
Radiogalaxy 5
BLlacHP 39
Seyfert3 5
HeIIRegions 55
使用的代码如下。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
f = open('hist.txt','r')
x,y = np.loadtxt(f,unpack=True, usecols=[0,1])
plt.hist(y, bins=10, histtype='bar',facecolor = 'red', rwidth=1.0)
plt.xlabel('Type of AGN',fontsize=15)
plt.ylabel('Number of galaxies',fontsize=15)
plt.title('Distribution of Type of active galaxies',fontsize=15)
plt.savefig('hist1.pdf',fmt='pdf')
plt.show()
我用同样的方法绘制带有数字的直方图。不确定我哪里出错了。
在 Mr.JohanC 的帮助下,我找到了问题的答案。
这是最后的程序
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
f = open('hist.txt','r')
x, y = np.loadtxt(f, unpack=True, usecols=[0, 1], dtype=str)
y = y.astype(int)
plt.bar(x, y)
plt.tick_params(axis='x', labelrotation=10, labelsize= 10)
plt.xlabel('Type of AGN',fontsize=15)
plt.ylabel('Number of galaxies',fontsize=15)
plt.title('Distribution of Type of active galaxies',fontsize=15)
plt.tight_layout()
plt.savefig('hist_gal.pdf',fmt='pdf')
plt.show()