有效管理多线图(45 条独特的线)
Effectively manage multiple line graphs(45 unique lines)
我的 groupby 挑战 我正在尝试使用折线图可视化 10 年来非洲国家(我的数据中有 45 个)的粮食生产。在使用 groupby 函数和 unstack 之后,绘图效果很好但不可读,并且区分每条线的颜色很差。从我讲师的可视化来看,他使用的是 Wolfram。
我如何使用 Python 实现此目的,或者我的方法有更好的替代方法吗?
这是我的代码
#To make the legend readable we reduce the font size
from matplotlib.font_manager import FontProperties
fontP = FontProperties()
fontP.set_size('small')
fig, ax = plt.subplots(figsize = (20,16))
df1.groupby(['Year','Country',]).sum().unstack().plot(ax = ax)
ax.set_yscale("log")
ax.set_ylim(1,300000)
plt.ylabel('Year')
plt.xlabel('Total Value')
plt.title('Food Production in Africa over the Years')
plt.legend(title='Countries', bbox_to_anchor=(1.05, 1), loc='upper left',
prop=fontP)
我的讲师使用 Wolfram 的可视化
我的尝试
由于数据不可用,我pseudo-created根据最新数据得出非洲每个国家的人口趋势,然后使用ax.text(x,y,country)
创建数据。它在你的数据中并没有那么拥挤,所以我认为它是适用的。此示例是根据 the official reference.
自定义的
import pandas as pd
import numpy as np
df = pd.read_csv('./africa_t.csv', sep=',')
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1, figsize=(12, 14))
# These are the colors that will be used in the plot
ax.set_prop_cycle(color=[
'#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a',
'#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94',
'#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d',
'#17becf', '#9edae5'])
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
fig.subplots_adjust(left=.06, right=.75, bottom=.02, top=.94)
ax.set_xticks(range(2011, 2020, 10))
ax.set_yticks(range(5000, 210000000, 5000000))
ax.xaxis.set_major_formatter('{x:.0f}')
ax.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
ax.tick_params(axis='both', which='both', labelsize=14,
bottom=False, top=False, labelbottom=True,
left=False, right=False, labelleft=True)
country = df.columns
for column in country:
line, = ax.plot(df['Year'].to_list(), df[column].to_list(), lw=2.5)
y_pos = df[column].to_list()[-1] - 0.5
ax.text(2020.5, y_pos, column, fontsize=14, color=line.get_color())
plt.show()
我的 groupby 挑战 我正在尝试使用折线图可视化 10 年来非洲国家(我的数据中有 45 个)的粮食生产。在使用 groupby 函数和 unstack 之后,绘图效果很好但不可读,并且区分每条线的颜色很差。从我讲师的可视化来看,他使用的是 Wolfram。
我如何使用 Python 实现此目的,或者我的方法有更好的替代方法吗?
这是我的代码
#To make the legend readable we reduce the font size
from matplotlib.font_manager import FontProperties
fontP = FontProperties()
fontP.set_size('small')
fig, ax = plt.subplots(figsize = (20,16))
df1.groupby(['Year','Country',]).sum().unstack().plot(ax = ax)
ax.set_yscale("log")
ax.set_ylim(1,300000)
plt.ylabel('Year')
plt.xlabel('Total Value')
plt.title('Food Production in Africa over the Years')
plt.legend(title='Countries', bbox_to_anchor=(1.05, 1), loc='upper left',
prop=fontP)
我的讲师使用 Wolfram 的可视化
我的尝试
由于数据不可用,我pseudo-created根据最新数据得出非洲每个国家的人口趋势,然后使用ax.text(x,y,country)
创建数据。它在你的数据中并没有那么拥挤,所以我认为它是适用的。此示例是根据 the official reference.
import pandas as pd
import numpy as np
df = pd.read_csv('./africa_t.csv', sep=',')
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1, figsize=(12, 14))
# These are the colors that will be used in the plot
ax.set_prop_cycle(color=[
'#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a',
'#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94',
'#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d',
'#17becf', '#9edae5'])
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
fig.subplots_adjust(left=.06, right=.75, bottom=.02, top=.94)
ax.set_xticks(range(2011, 2020, 10))
ax.set_yticks(range(5000, 210000000, 5000000))
ax.xaxis.set_major_formatter('{x:.0f}')
ax.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
ax.tick_params(axis='both', which='both', labelsize=14,
bottom=False, top=False, labelbottom=True,
left=False, right=False, labelleft=True)
country = df.columns
for column in country:
line, = ax.plot(df['Year'].to_list(), df[column].to_list(), lw=2.5)
y_pos = df[column].to_list()[-1] - 0.5
ax.text(2020.5, y_pos, column, fontsize=14, color=line.get_color())
plt.show()