利用matplotlib和pandas绘制有柱状图、折线图和饼图构成的组合图import pandas as pdimport matplotlib.pyplot as plt# 创建数据data = {'apple': [3, 2, 4, 5], 'orange': [1, 5, 2, 6], 'banana': [2, 3, 1, 4]}df = pd.DataFrame(data, index=['Jan', 'Feb', 'Mar', 'Apr'])# 创建子图fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))# 绘制柱状图df.plot(kind='bar', ax=axs[0])axs[0].set_title('Fruit sales')# 绘制折线图df.plot(kind='line', ax=axs[1])axs[1].set_title('Fruit sales')# 绘制饼图plt.figure(figsize=(5,5))df.sum().plot(kind='pie', autopct='%1.1f%%')plt.title('Fruit sales')# 显示图形plt.show(),data = {'w':[126743, 127627, 128453, 129227, 129988, 130756, 131448, 132129, 132802, 133450, 134091, 134735, 135404, 136072, 136782, 137462, 138271, 139008, 139538, 140005], 'a':[140005, 23493, 98914, 17599], 'b':[139538, 23523, 99357, 16658]} df = pd.DataFrame(data, index=['w', 'a', 'b'])值的长度与索引长度不匹配怎么改
时间: 2023-10-23 11:31:03 浏览: 269
Matplotlib绘图资料,转运资料
如果值的长度与索引长度不匹配,可以在DataFrame中使用字典时,将值的长度与索引长度保持一致。如果数据不足,可以使用NaN来填充缺失的数据。例如,可以将上面的数据中'a'和'b'的值的长度与'w'保持一致,使用NaN填充缺失的数据,如下所示:
data = {'w':[126743, 127627, 128453, 129227, 129988, 130756, 131448, 132129, 132802, 133450, 134091, 134735, 135404, 136072, 136782, 137462, 138271, 139008, 139538, 140005], 'a':[140005, 23493, 98914, 17599, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN], 'b':[139538, 23523, 99357, 16658, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN]}
df = pd.DataFrame(data, index=['w', 'a', 'b'])
阅读全文