【问题标题】:Effectively manage multiple line graphs(45 unique lines)有效管理多条折线图(45条独特的线)
【发布时间】:2020-08-23 10:51:32
【问题描述】:

我的 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 进行可视化

我的尝试

【问题讨论】:

  • 欢迎来到 Stackoverflow。你的尝试很好。您也应该在上面发布您的 python 代码,以便这里的用户可以提供建议。或者您是否在询问可用于这些数据的其他图形替代方案?

标签: python data-visualization linegraph


【解决方案1】:

由于数据不可用,我根据最新数据伪创建了非洲每个国家/地区的人口趋势,然后使用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 函数如何集成循环,请帮助
  • 生成的分组数据帧将在循环过程中绘制。生成的分组数据的形状必须是我粘贴的数据图像的形状。行是年份,列是按国家/地区划分的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多