简体   繁体   English

来自 .txt 文件的数据的 Plot 直方图,其中 x 轴上的名称和 y 轴上的数字

[英]Plot histogram for data from .txt file with names on x-axis and number on y axis

I am new to programming, and I have tried plotting histograms for values.我是编程新手,我尝试为值绘制直方图。 This is the first time I am trying to plot a histogram for names on the x axis.这是我第一次尝试 plot x 轴上的名称直方图。 I went through the posts but found none helpful.我浏览了这些帖子,但没有发现任何帮助。 I will greatly appreciate and be indebted to your help.我将非常感谢并感谢您的帮助。

The data saved in a .txt file保存在.txt文件中的数据

Data数据

Seyfert1    112
Seyfert2    42
Quasars     131
QuasarsSeyfert1 46
Radiogalaxy    5
BLlacHP       39
Seyfert3    5
HeIIRegions 55

The code used is given below.使用的代码如下所示。

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

I used the same for plotting the histogram with numbers.我用同样的方法用数字绘制直方图。 Not sure where I am going wrong.不知道我哪里出错了。

With the help of inputs from Mr.JohanC, I have found the answer to the question posted.在 Mr.JohanC 的帮助下,我找到了问题的答案。

Here is the final program这是最后的程序

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

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

相关问题 使用txt文件(Python)中的数据绘制日期和时间(x轴)与值(y轴)的关系 - Plot date and time (x axis) versus a value (y axis) using data from txt file (Python) 以列名作为 x 轴绘制直方图 - Plotting a histogram with the column names as the x-axis 使用txt文件(Python)中的数据绘制日期和时间(x轴)与值(y轴)(仅用空格分隔的列) - Plot date and time (x axis) versus a value (y axis) using data from txt file (Python) (columns separated only by spaces) 直方图:X 轴和 Y 轴值显示不正确 - histogram : X-axis & Y-axis values not displaying correctly 如何在Python中从数据框绘制带有x和y轴的直方图 - How to plot histogram with x and y axis from dataframe in python 使用排序文件绘制 X 轴与原始文件中相应的 Y 值 - Using sorted file to plot X-axis with corresponding Y-values from the original file 如何在 python 中以像素数为 x 轴,灰度颜色为 y 轴 plot 图形? - How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python? 在日志中修复 x 轴和 y 轴 plot python - Fixing x-axis and y-axis in a log plot python 如何在y轴上绘制日期,在x轴上绘制小时? - How to plot date on y-axis and hours on x-axis? 将绘图的y轴和x轴比率设置为相等 - Set the plot y-axis and x-axis ratio equal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM