- Hands-On Exploratory Data Analysis with Python
- Suresh Kumar Mukhiya Usman Ahmed
- 303字
- 2021-06-24 16:44:54
Lollipop chart
A lollipop chart can be used to display ranking in the data. It is similar to an ordered bar chart.
Let's consider the carDF dataset. It can be found in the GitHub repository for chapter 2. Alternatively, it can be used from the GitHub link directly, as mention in the following code:
Load the dataset:
#Read the dataset
carDF = pd.read_csv('https://raw.githubusercontent.com/PacktPublishing/hands-on-exploratory-data-analysis-with-python/master/Chapter%202/cardata.csv')
2.roup the dataset by manufacturer. For now, if it does not make sense, just remember that the following snippet groups the entries by a particular field (we will go through groupby functions in detail in Chapter 4, Data Transformation):
#Group by manufacturer and take average mileage
processedDF = carDF[['cty','manufacturer']].groupby('manufacturer').apply(lambda x: x.mean())
3.ort the values by cty and reset the index (again, we will go through sorting and how we reset the index in Chapter 4, Data Transformation):
#Sort the values by cty and reset index
processedDF.sort_values('cty', inplace=True)
processedDF.reset_index(inplace=True)
4.lot the graph:
#Plot the graph
fig, ax = plt.subplots(figsize=(16,10), dpi= 80)
ax.vlines(x=processedDF.index, ymin=0, ymax=processedDF.cty, color='firebrick', alpha=0.7, linewidth=2)
ax.scatter(x=processedDF.index, y=processedDF.cty, s=75, color='firebrick', alpha=0.7)
5.nnotate the title:
#Annotate Title
ax.set_title('Lollipop Chart for Highway Mileage using car dataset', fontdict={'size':22})
6.nnotate labels, xticks, and ylims:
ax.set_ylabel('Miles Per Gallon')
ax.set_xticks(processedDF.index)
ax.set_xticklabels(processedDF.manufacturer.str.upper(), rotation=65, fontdict={'horizontalalignment': 'right', 'size':12})
ax.set_ylim(0, 30)
7.rite the actual mean values in the plot, and display the plot:
#Write the values in the plot
for row in processedDF.itertuples():
ax.text(row.Index, row.cty+.5, s=round(row.cty, 2), horizontalalignment= 'center', verticalalignment='bottom', fontsize=14)
#Display the plot on the screen
plt.show()
The lollipop chart generated by the preceding snippet is as follows:

Having seen the preceding output, you now know why it is called a lollipop chart, don't you? The line and the circle on the top gives a nice illustration of different types of cars and their associated miles per gallon consumption. Now, the data makes more sense, doesn't it?
- Blender 3D Incredible Machines
- Python自然語言處理(微課版)
- Python數(shù)據(jù)可視化之Matplotlib與Pyecharts實戰(zhàn)
- 精通Python設(shè)計模式(第2版)
- D3.js 4.x Data Visualization(Third Edition)
- Express Web Application Development
- 常用工具軟件立體化教程(微課版)
- C語言開發(fā)基礎(chǔ)教程(Dev-C++)(第2版)
- Hands-On Nuxt.js Web Development
- JavaScript+jQuery網(wǎng)頁特效設(shè)計任務(wù)驅(qū)動教程
- Building Dynamics CRM 2015 Dashboards with Power BI
- Mastering Concurrency in Python
- Secret Recipes of the Python Ninja
- Maven for Eclipse
- Continuous Delivery and DevOps:A Quickstart Guide Second Edition