【问题标题】:How to show bar chart in python如何在python中显示条形图
【发布时间】:2019-11-18 21:03:39
【问题描述】:

我正在尝试在 Python 中创建一个包含多个条形的条形图。条形图应在每个条形顶部显示值。 我有如下数据集:

Speciality                  Very interested Somewhat_interested Notinterested
Big Data (Spark/Hadoop)         1332           729                      127
Data Analysis / Statistics      1688           444                      60     
Data Journalism                 429            1081                     610

我已经尝试了以下代码:

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt

pd_dataframe = pd.read_csv('Test-Barchart.csv')
no_of_xaxis = pd_dataframe.Speciality.nunique()
ind = np.arange(no_of_xaxis)
xcord = pd_dataframe['Speciality'].tolist()

veryinterestedlist = pd_dataframe['Very interested'].tolist()
somewhatlist = pd_dataframe['Somewhat interested'].tolist()
notinterestedlist = pd_dataframe['Not interested'].tolist()

fig=plt.figure()
ax = fig.add_subplot(111)
width=0.8

rects1 = ax.bar(ind, veryinterestedlist, width, color='r')
rects2 = ax.bar(ind, somewhatlist, width, color='g')
rects3 = ax.bar(ind+width*2, notinterestedlist, width, color='b')

ax.legend( (rects1[0], rects2[0], rects3[0]), ('Very Interested', 
 'Somewhat Interested', 'Not Interested') )

def autolabel(rects):
   for rect in rects:
    h = rect.get_height()
    ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h),
            ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

ax.set_xticks(ind+width)
ax.set_xticklabels( xcord )
plt.show()

问题是,plt.show() 没有显示任何内容! 我的代码没有任何错误。 你能帮我解决这个问题吗? 另外,如何将条形颜色更改为十六进制代码颜色而不是 r、g 或 b?例如#5bc0de

【问题讨论】:

  • 事实上,运行你的代码,我得到了一个图表。一个凌乱的图表,但仍然是一个图表。你确定你发布了你的实际代码吗?关于十六进制代码的问题,只需将color='g'替换为color='#5bc0de'即可。
  • 是的,这是实际代码。我需要有条形图。在 x 轴专业上,如大数据、数据分析......对于每个专业,我需要显示三个不同颜色的条形图。感兴趣的栏,有点感兴趣的栏和不感兴趣的栏
  • 当然,由于列标题不匹配,您的代码或数据中都有错误。无论如何,请确保您使用的是交互式后端。

标签: python python-3.x pandas matplotlib bar-chart


【解决方案1】:

对代码的小改动:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

pd_dataframe = pd.read_csv('Test-Barchart.csv')
no_of_xaxis = pd_dataframe.Speciality.nunique()
ind = np.arange(no_of_xaxis)
width = 0.1
xcord = pd_dataframe['Speciality'].tolist()

veryinterestedlist = pd_dataframe['Very interested'].tolist()
somewhatlist = pd_dataframe['Somewhat interested'].tolist()
notinterestedlist = pd_dataframe['Not interested'].tolist()

fig, ax = plt.subplots()

rects1 = ax.bar(ind, veryinterestedlist, width, color='g')
rects2 = ax.bar(ind + width, somewhatlist, width, color='c')
rects3 = ax.bar(ind+2*width, notinterestedlist, width, color='r')

# add some text for labels, title and axes ticks
ax.set_ylabel('y label')
ax.set_title('Title')
ax.set_xticks(ind + width)
ax.set_xticklabels(xcord)

ax.legend( (rects1[0], rects2[0], rects3[0]), ('Very Interested', 
 'Somewhat Interested', 'Not Interested') )

def autolabel(rects):
    """
    Attach a text label above each bar displaying its height
    """
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

plt.show()

你会得到:

参考:Grouped bar chart with labels

【讨论】:

  • 非常感谢。它现在正在工作。如何使用十六进制代码颜色为每个条分配不同的颜色,例如##5bc0de
  • 只需将color='c'替换为color='#5bc0de'即可,例如:rects2 = ax.bar(ind + width, somewhatlist, width, color='#5bc0de')
  • 它工作正常。非常感谢。很抱歉问了很多问题,但我还是初学者。显示的条形图有很多格式问题,如下图:
  • 使用width = 0.1
  • 要求宽度保持在0.8
猜你喜欢
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
相关资源
最近更新 更多