尝试根据以下数据创建饼图和条形图 [英] Trying to create a pie and bar chart from the data below

查看:52
本文介绍了尝试根据以下数据创建饼图和条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用下面的代码创建饼图和条形图.任何人都可以帮忙吗?我们做错了什么?

We're trying to create a pie chart, and a bar chart from the code below. Can anyone help? And what are we doing wrong?

任何提供的帮助将不胜感激.谢谢.

Any help provided would be greatly appreciated. Thanks.

browser = ({'IE': [0.21], 'Chrome': [0.51], 'Firefox': [0.18], 'Safari': [0.06], 'Others': [0.04]})
x = pd.Series(browser)
y = pd.Series.sort_values(x)
z = pd.DataFrame(y)
fig, axes = plt.subplots(nrows=1, ncols=2)
z.plot(kind = 'pie', ax = axes[0,0])
z.plot(kind = 'bar', ax - axes[0,1])

推荐答案

您的代码中有两个错误.请参阅下面有关如何绘制饼图的注释代码.

There are a couple of mistakes in your code. See commented code below on how to plot the pie chart.

import pandas as pd
import matplotlib.pyplot as plt

# dont put the dictionary into a tuple
browser = {'IE': [0.21], 'Chrome': [0.51], 'Firefox': [0.18], 'Safari': [0.06], 'Others': [0.04]}
# dont use values inside a list as column values for a dataframe
browser2 = {}
[browser2.update({key : val[0]}) for key, val in browser.iteritems()]

x = pd.Series(browser2)
y = pd.Series.sort_values(x)
z = pd.DataFrame(y)
print z
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8,4))
# you need to specify which column of the dataframe you want to plot (here 0)
z.plot(y=0, kind = 'pie', ax = axes[0])
z.plot(kind = 'bar', ax = axes[1])
axes[0].set_aspect("equal") # make aspect equal (such that circle is not eliptic)
#place the legend at a decent position
axes[0].legend(loc=1, bbox_to_anchor= (1,0.2), fontsize=11)
plt.tight_layout()
plt.show()

这篇关于尝试根据以下数据创建饼图和条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆