Pie and Donut chart- code done

pull/354/head
Jasleen Sondhi 3 years ago committed by GitHub
parent 17e79eb24d
commit f2666cf99c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -58,34 +58,32 @@ The output is:
``` ```
Take this data and convert the 'class' column to a category: Take this data and convert the 'class' column to a category:
```python ```r
cols = mushrooms.select_dtypes(["object"]).columns grouped=mushrooms %>%
mushrooms[cols] = mushrooms[cols].astype('category') group_by(class) %>%
summarise(count=n())
``` ```
```python
edibleclass=mushrooms.groupby(['class']).count()
edibleclass
```
Now, if you print out the mushrooms data, you can see that it has been grouped into categories according to the poisonous/edible class: Now, if you print out the mushrooms data, you can see that it has been grouped into categories according to the poisonous/edible class:
```r
View(grouped)
```
| class | count |
| --------- | --------- |
| Edible | 4208 |
| Poisonous| 3916 |
| | cap-shape | cap-surface | cap-color | bruises | odor | gill-attachment | gill-spacing | gill-size | gill-color | stalk-shape | ... | stalk-surface-below-ring | stalk-color-above-ring | stalk-color-below-ring | veil-type | veil-color | ring-number | ring-type | spore-print-color | population | habitat |
| --------- | --------- | ----------- | --------- | ------- | ---- | --------------- | ------------ | --------- | ---------- | ----------- | --- | ------------------------ | ---------------------- | ---------------------- | --------- | ---------- | ----------- | --------- | ----------------- | ---------- | ------- |
| class | | | | | | | | | | | | | | | | | | | | | |
| Edible | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | ... | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 | 4208 |
| Poisonous | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | ... | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 | 3916 |
If you follow the order presented in this table to create your class category labels, you can build a pie chart: If you follow the order presented in this table to create your class category labels, you can build a pie chart.
## Pie! ## Pie!
```python ```r
labels=['Edible','Poisonous'] pie(grouped$count,grouped$class, main="Edible?")
plt.pie(edibleclass['population'],labels=labels,autopct='%.1f %%')
plt.title('Edible?')
plt.show()
``` ```
Voila, a pie chart showing the proportions of this data according to these two classes of mushrooms. It's quite important to get the order of the labels correct, especially here, so be sure to verify the order with which the label array is built! Voila, a pie chart showing the proportions of this data according to these two classes of mushrooms. It's quite important to get the order of the labels correct, especially here, so be sure to verify the order with which the label array is built!
@ -97,26 +95,29 @@ A somewhat more visually interesting pie chart is a donut chart, which is a pie
Take a look at the various habitats where mushrooms grow: Take a look at the various habitats where mushrooms grow:
```python ```r
habitat=mushrooms.groupby(['habitat']).count() habitat=mushrooms %>%
habitat group_by(habitat) %>%
summarise(count=n())
View(habitat)
``` ```
Here, you are grouping your data by habitat. There are 7 listed, so use those as labels for your donut chart: The output is:
| habitat| count |
```python | --------- | --------- |
labels=['Grasses','Leaves','Meadows','Paths','Urban','Waste','Wood'] | Grasses | 2148 |
| Leaves| 832 |
plt.pie(habitat['class'], labels=labels, | Meadows | 292 |
autopct='%1.1f%%', pctdistance=0.85) | Paths| 1144 |
| Urban | 368 |
center_circle = plt.Circle((0, 0), 0.40, fc='white') | Waste| 192 |
fig = plt.gcf() | Wood| 3148 |
fig.gca().add_artist(center_circle)
plt.title('Mushroom Habitats') Here, you are grouping your data by habitat. There are 7 listed, so use those as labels for your donut chart:
plt.show() ```r
library(webr)
PieDonut(habitat, aes(habitat, count=count))
``` ```
![donut chart](images/donut-wb.png) ![donut chart](images/donut-wb.png)

Loading…
Cancel
Save