【问题标题】:issue in making a bar chart using matplotlib or mpld3 in pyspark在 pyspark 中使用 matplotlib 或 mpld3 制作条形图的问题
【发布时间】:2015-08-14 16:17:38
【问题描述】:

我有一个列表 list = [['0-50',4],['50-100',11],['100-150',73],['150-200',46]],我想在 python pyspark 中使用 mpld3histogram 上显示它。列表中每个元素的第一部分是范围,它将位于histogram 的 x 轴上,第二部分是该范围内的人数,它将位于 y 轴上。如何在pyspark 中使用matplotlibmpld3 制作条形图?

更新:我根据 [this] 示例 1 尝试了下面的代码,它显示了 bar chart,但输出在视觉上非常糟糕,绘图边界周围有很多灰色区域。我怎样才能让它在可视化方面看起来更清晰更好?

import numpy as np
import matplotlib.pyplot as plt

list = [['0-50',4],['50-100',11],['100-150',73],['150-200',46]]
n_groups = len(list)

fig, ax = plt.subplots()

index = np.arange(n_groups)
bar_width = 0.35

opacity = 0.4
error_config = {'ecolor': '0.3'}

number = []
ranges = []
for item in list:
    number.append(item[1])
    ranges.append(item[0])

rects1 = plt.bar(index, number, bar_width,
                 alpha=opacity,
                 color='b',
                 error_kw=error_config)

plt.xlabel('Number')
plt.ylabel('range')
plt.xticks(index + bar_width, (ranges[0],ranges[1],ranges[2],ranges[3]))
plt.legend()

plt.tight_layout()
plt.show()

【问题讨论】:

标签: python matplotlib histogram pyspark mpld3


【解决方案1】:

matplotlib 的情节看起来不错的秘密武器是import seaborn。这会覆盖 mpl 的默认值,并提供一些不错的东西。

我还会将条形变大并将 xticks 移动到条形的中间。为此,您需要对代码稍作调整:

import numpy as np, matplotlib.pyplot as plt, mpld3, seaborn as sns

list = [['0-50',4],['50-100',11],['100-150',73],['150-200',46]]
n_groups = len(list)
index = np.arange(n_groups)

bar_width = 0.9
opacity = 0.4

number = []
ranges = []
for item in list:
    number.append(item[1])
    ranges.append(item[0])

rects1 = plt.bar(index, number, bar_width,
                 alpha=opacity,
                 color='b')

plt.xlabel('Number')
plt.ylabel('range')
plt.xticks(index + bar_width/2, (ranges[0],ranges[1],ranges[2],ranges[3]))

mpld3.display()

这是它的外观:

还有一个笔记本where you can see the interactivity that mpld3 adds(基本没啥用,不过有点好玩)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多