Skip to content Skip to sidebar Skip to footer

Trying To Create A Pie And Bar Chart From The Data Below

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. brows

Solution 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()

enter image description here

Post a Comment for "Trying To Create A Pie And Bar Chart From The Data Below"