简体   繁体   English

如何 plot 分散 plot 以获取范围数据

[英]How to plot scatter plot for range data

I'm having range data for example an educational institute is charging fee for batch of students for online courses.我有范围数据,例如,一家教育机构正在向一批学生收取在线课程的费用。

If 1-4 students join the fee is 10000/per batch 1-4名学生报名费用为10000/批

If 5-10 students join the fee is 15000/per batch 5-10名学生报名费用为15000/批

If 11-20 students join the fee is 22000/per batch 11-20人报名费用为22000/批

x = ['1-4','5-10','11-20']
y = [10000,15000,20000]

The x and y is my xlable and ylable for matplotlib. x 和 y 是 matplotlib 的 xlable 和 ylable。 In this case how to transform the data of x and plot as xlable.在这种情况下如何将 x 和 plot 的数据转换为 xlable。

The x and y arrays can be converted into arrays of numeric values that can be used to create a scatter plot: xy arrays 可以转换为可用于创建散点 plot 的数值的 arrays:

  1. convert the x string into ranges将 x 字符串转换为范围
x_ranges = [list(range(int(xi[0]), int(xi[1])+1)) for xi in [xi.split('-') for xi in x]]
#[[1, 2, 3, 4], [5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]]
  1. add one y element per entry in the corresponding x range在对应的 x 范围内为每个条目添加一个 y 元素
y_expanded = [(x[0], [x[1]]*len(x[0])) for x in zip(x_ranges,y)]
#[([1, 2, 3, 4], [10000, 10000, 10000, 10000]),
# ([5, 6, 7, 8, 9, 10], [15000, 15000, 15000, 15000, 15000, 15000]),
# ([11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
#  [20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000])]
  1. re-group the x and y arrays重新组合 x 和 y arrays
xy_sorted = list(map(list, zip(*y_expanded)))
#[[[1, 2, 3, 4], [5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]],
# [[10000, 10000, 10000, 10000],
#  [15000, 15000, 15000, 15000, 15000, 15000],
#  [20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000]]]
  1. flatten the list for the x and y values展平 x 和 y 值的列表
x_result = [x for sublist in xy_sorted[0] for x in sublist]
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
y_result = [y for sublist in xy_sorted[1] for y in sublist]
#[10000, 10000, 10000, 10000, 15000, 15000, ...]
  1. create the scatter plot创建散点图 plot
plt.xticks(x_result)
plt.ylim(0, max(y_result)+1000)
plt.scatter(x_result, y_result)
plt.show()

散点图

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

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