Drawing a lollipop chart with Python, it's really nice!

Hello everyone, I'm Xiao F~

A bar chart is a frequently used chart in data visualization.

Although it is very useful, there are still shortcomings. For example, when there are too many bar chart entries, it will appear bloated and unintuitive.

The lollipop chart is an improvement on the bar chart, with a small and fresh design, which clearly expresses our data.

The following small F will introduce to you, how to use Python to draw a lollipop chart.

The data of the birth population in my country from 1949 to 2019 are used, and the data comes from the National Bureau of Statistics.

First read the data.

import pandas as pd
import matplotlib.pyplot as plt

# 读取数据
df = pd.read_csv('data.csv')
print(df)

The results are as follows.

The dataset is simple, each row has only one year and one value.

Start by plotting a bar chart with the values ​​for each year.

# 绘制柱状图
plt.bar(df.Year, df.value)
plt.show()

Two lines of code to get a bar chart, which does seem a bit crowded.

The data for the last year, 2019, is differentiated below.

Color the bars black for 2019 and light gray for other years.

And adding a scatter plot to the chart draws circles on top of the bars.

# 新建画布
fig, ax = plt.subplots(1, figsize=(12, 8))

# 年份数
n = len(df)
# 颜色设置
colors = ['black'] + ((n-1)*['lightgrey'])
plt.bar(df.Year, df.value, color=colors)
plt.scatter(df.Year, df.value, color=colors)
plt.show()

The results obtained are as follows.

The color has been modified successfully, and the width of the bar chart and the size of the top circle need to be adjusted.

# width: 条形图宽度  s: 散点图圆圈大小
plt.bar(df.Year, df.value, color=colors, width=0.2)
plt.scatter(df.Year, df.value, color=colors, s=10)
plt.show()

The results are as follows.

Compared to the previous blue bar chart, the lollipop chart is indeed a lot better.

Instead of using bar charts for lollipop charts, you can also use lines so that the overall width will be more consistent.

X uses Year (year) data as the start and end points, and Y uses -20 and the data of each year as the start and end points.

import pandas as pd
import matplotlib.pyplot as plt

# 读取数据
df = pd.read_csv('data.csv')
print(df)

# 新建画布
fig, ax = plt.subplots(1, figsize=(12, 8))

# 年份数
n = len(df)
# 颜色设置
colors = ['black'] + ((n-1)*['lightgrey'])
# 使用线条
for idx, val in df.iterrows():
    plt.plot([val.Year, val.Year],
             [-20, val.value],
             color=colors[idx])
plt.show()

The results obtained are as follows.

Instead of just generating a scatter plot on top, you can use parameter markers to draw circles at the ends.

The bottommost circle can then be hidden by changing the y-limit parameter.

# 新建画布
fig, ax = plt.subplots(1, figsize=(12, 8))

# 年份数
n = len(df)
# 颜色设置
colors = ['black'] + ((n-1)*['lightgrey'])
# 使用线条, markersize设置标记点大小
for idx, val in df.iterrows():
    plt.plot([val.Year, val.Year],
             [-20, val.value],
             color=colors[idx],
             marker='o',
             markersize=3)

# 设置y轴最低值
plt.ylim(0,)
plt.show()

The results are as follows.

In addition, you can adjust the lw, markersize parameters, define the thickness of the line and the size of the marker, and even draw the line twice to create an outline effect.

# 新建画布
fig, ax = plt.subplots(1, figsize=(12, 8))
color = 'b'

# 年份数
n = len(df)
# 颜色设置
colors = ['black'] + ((n-1)*['lightgrey'])
# 使用线条
for idx, val in df.iterrows():
    plt.plot([val.Year, val.Year],
             [-20, val.value],
             color='black',
             marker='o',
             lw=4,
             markersize=6)
    plt.plot([val.Year, val.Year],
             [-20, val.value],
             color=colors[idx],
             marker='o',
             markersize=4)

# 移除上边框、右边框
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

# 设置x、y轴范围
plt.xlim(1948, 2020)
plt.ylim(0,)

# 中文显示
plt.rcParams['font.sans-serif'] = ['Songti SC']

plt.title('中国历年出生人口数据(万)', loc='left', fontsize=16)
plt.text(2019, -220, '来源:国家统计局', ha='right')

# 2019年出生人口数(显示)
value_2019 = df[df['Year'] == 2019].value.values[0]
plt.text(2019, value_2019+80, value_2019, ha='center')

# 保存图片
plt.savefig('chart.png')

The results obtained are as follows.

Black is not particularly good-looking, change the color to see.

# 新建画布
fig, ax = plt.subplots(1, figsize=(12, 8))

# 年份数
n = len(df)
# 颜色设置
color = 'b'
colors = ['#E74C3C'] + ((len(df)-1)*['#F5B7B1'])
# 使用线条
for idx, val in df.iterrows():
    plt.plot([val.Year, val.Year],
             [-20, val.value],
             color=colors[idx],
             marker='o',
             lw=4,
             markersize=6,
             markerfacecolor='#E74C3C')

# 移除上边框、右边框
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

# 设置x、y轴范围
plt.xlim(1948, 2020)
plt.ylim(0,)

# 中文显示
plt.rcParams['font.sans-serif'] = ['Songti SC']

plt.title('中国历年出生人口数据(万)', loc='left', fontsize=16)
plt.text(2019, -220, '来源:国家统计局', ha='right')

# 2019年出生人口数(显示)
value_2019 = df[df['Year'] == 2019].value.values[0]
plt.text(2019, value_2019+80, value_2019, ha='center')

# 保存图片
plt.savefig('chart.png')

The results obtained are as follows.

Now for bar charts, you have another option, the lollipop chart.

In addition, we can also know that the number of new born population in China is getting smaller and smaller. It is said that the birth population in 2020 will drop by more than 10%, and it may fall below 10 million in the next few years...

Finally, reply to " Lollipop " on the official account, and get the code and data used this time, and you can learn by yourself.

  end book  

This time, Xiao F and [Peking University Press] bring you 5 books related to Python big data.

" Python big data analysis from entry to mastery " teaches you to easily play big data analysis through 3-layer technical architecture + 3 sets of classic data + 5 Python libraries of big data platform tools/engines + 2 integration directions! Click the image below to see details/purchase????

Book donation rules : After you like this article ("I'm watching" is not required), scan the QR code below and add Xiao F's WeChat. Send me the screenshots of the likes, and I will send you the lucky draw code until 21:00 on May 10th.

Thank you all for your support of F!

Recommended reading

···  END  ···

Guess you like

Origin blog.csdn.net/river_star1/article/details/116573956