import pandas as pd
#data
d = {'ageFrom': [0, 11, 21, 31], 'ageTo': [10, 20, 30, 40], 'sales':[2, 10, 15, 20]}
#create dataframe
df = pd.DataFrame(data = d)
#create a mean value for your range
df['mean'] = (df['ageFrom'] + df['ageTo'])/2
#plot data
ax1 = df.plot.scatter(x='mean',y='sales')
或:您仍然可以创建范围列并将其绘制为条形图,如下所示:
#create a range column
df['range'] = df['ageFrom'].astype(str) + '-' + df['ageTo'].astype(str)
#plot bar
ax2 = df.plot.bar(x='range', y='sales', rot=0)
最后您仍然可以使用matplotlib为x轴使用非数字,如下所示:
import matplotlib.pyplot as plt
plt.scatter(df.range,df['sales'],c='b',label='range')
现在我不确定你有/没有尝试过什么,但是最简单的方法是为你的范围创建一个平均值列(如果你想创建一个范围,避免使用非数值),然后根据你的销售额绘制它。以下各项应能做到这一点:
或:您仍然可以创建范围列并将其绘制为条形图,如下所示:
最后您仍然可以使用matplotlib为x轴使用非数字,如下所示:
相关问题 更多 >
编程相关推荐