简体   繁体   English

Matplotlib-如何在x值是每个y值的频率的位置绘制图?

[英]Matplotlib-How do I plot a graph where the x values are the frequency of each y value?

If I'm trying to plot a graph of the number of people born in each month, and I have a list like: 如果我试图绘制每个月出生人数的图表,并且我有一个列表,例如:

[ {"bob": "January"},{"Peter":"February"},{"Mary":"March"}, {"John":"March"}, {"Sully": "March"}, {"Kai":"April"} ,{"Jerry": "February"}, {"Polly":"may"}, {"Nina": "June"}, {"Mic": "July"}, {"Paul": "August"}, {"Ferry": "January"}]

What is the best way to plot a graph with the months on the x axis and the number of people who are born in that month on the y axis? 在x轴上以月​​为单位,在y轴上以该月的出生人数绘制图表的最佳方法是什么? I have the y values which are the months, but I'm having trouble with the x values. 我的y值是月份,但x值有问题。 Do I have to go through the list and calculate the frequency of each month first? 我是否必须仔细查看清单并首先计算每个月的频率? I will be working with a much bigger list with more than a thousand items and need a solution that is efficient. 我将处理一个更大的列表,其中包含一千多个项目,并且需要一个有效的解决方案。

You can use ChainMap to first flatten your list of dictionaries and then apply Counter module to get the frequency of names for each month. 您可以使用ChainMap首先展平字典列表,然后应用Counter模块获取每个月的名字出现频率。 Then you can extract the month names and the corresponding frequencies and plot them using a bar chart. 然后,您可以提取月份名称和相应的频率,并使用条形图进行绘制。 Below is a sample answer using your provided data. 以下是使用您提供的数据的示例答案。 If you want to sort the month names on the x-axis, you can use the answer provided here . 如果要在x轴上对月份名称进行排序,可以使用此处提供的答案。

from collections import Counter, ChainMap
import matplotlib.pyplot as plt

figure = plt.figure(figsize=(8, 6))

dictionary = [{"bob": "January"},{"Peter":"February"},{"Mary":"March"}, {"John":"March"}, 
                  {"Sully": "March"}, {"Kai":"April"} ,{"Jerry": "February"}, {"Polly":"may"}, 
                  {"Nina": "June"}, {"Mic": "July"}, {"Paul": "August"}, {"Ferry": "January"}]

dict_flat = ChainMap(*dictionary)

frequency = Counter(dict_flat.values()).most_common()
x, y = zip(*frequency)

plt.bar(x, y, width=0.5)
plt.show()

在此处输入图片说明

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

相关问题 对于折线图(matplotlib,pandas)的 dataframe 中缺少 x 值,如何将 y 值设置为 0 - How do I set y value as 0 for missing x values in dataframe for line graph (matplotlib, pandas) 如何在matplotlib中绘制多个X或Y轴? - How do I plot multiple X or Y axes in matplotlib? 如何在同一图表上使用 matplotlib 到 plot 2 组 (x,y) 值作为散点 plot(由线连接)? - How to use matplotlib to plot 2 sets of (x,y) values on the same graph as a scatter plot (connected by lines)? Matplotlib:使用 tkinter 隐藏 plot 音频图上的 Y 和 X 值 - Matplotlib: Hide Y and X values on a plot audio graph with tkinter Matplotlib 散点图 plot,每个 x 的 y 值数组 - Matplotlib scatter plot with array of y values for each x 在 Python 中,如何为不等距/随机 y 值绘制彩色编码的二维图(X、Y、颜色)? - In Python, how do I plot a color-coded 2-D graph (X, Y, color) for inequally-spaced / random y values? 如何在现有图上的matplotlib中绘制(x,y)值? - How to plot (x,y) values in matplotlib on an existing plot? 如何在matplotlib的折线图中绘制最大值点? - How do I plot the point of max value on a line graph in matplotlib? 如何在matplotlib中的绘图上删除x,y轴上的值 - How to remove values on x,y axis on plot in matplotlib 如何使用matplotlib在图形X轴上绘制分数值? - How to plot fraction values in a graph x-axis using matplotlib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM