相关疑难解决方法(0)

在matplotlib条形图上添加值标签

我被困在一些感觉应该相对容易的事情上.我下面的代码是基于我正在研究的更大项目的示例.我没有理由发布所有细节,所以请接受我带来的数据结构.

基本上,我正在创建一个条形图,我只是想弄清楚如何在条形图上添加值标签(在条形图的中心,或者在它上面).一直在寻找网络上的样本,但没有成功实现我自己的代码.我相信解决方案要么是'text',要么是'annotate',但是我:a)不知道使用哪一个(一般来说,还没弄清楚何时使用哪个).b)无法看到要么呈现价值标签.非常感谢您的帮助,我的代码如下.提前致谢!

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.set_option('display.mpl_style', 'default') 
%matplotlib inline

# Bring some raw data.
frequencies = [6, 16, 75, 160, 244, 260, 145, 73, 16, 4, 1]

# In my original code I create a series and run on that, 
# so for consistency I create a series from the list.
freq_series = pd.Series.from_array(frequencies)

x_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0, 
            121740.0, 123980.0, 126220.0, 128460.0, 130700.0]

# Plot the figure. …
Run Code Online (Sandbox Code Playgroud)

python data-visualization matplotlib python-2.7 pandas

74
推荐指数
5
解决办法
13万
查看次数

如何绘制和注释分组条形图

我遇到了一个关于 Python 中 matplotlib 的棘手问题。我想创建一个包含多个代码的分组条形图,但图表出错了。您能给我一些建议吗?代码如下。

import numpy as np
import pandas as pd
file="https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/coursera/Topic_Survey_Assignment.csv"
df=pd.read_csv(file,index_col=0)

df.sort_values(by=['Very interested'], axis=0,ascending=False,inplace=True)

df['Very interested']=df['Very interested']/2233
df['Somewhat interested']=df['Somewhat interested']/2233
df['Not interested']=df['Not interested']/2233
df

df_chart=df.round(2)
df_chart

labels=['Data Analysis/Statistics','Machine Learning','Data Visualization',
       'Big Data (Spark/Hadoop)','Deep Learning','Data Journalism']
very_interested=df_chart['Very interested']
somewhat_interested=df_chart['Somewhat interested']
not_interested=df_chart['Not interested']

x=np.arange(len(labels))
w=0.8

fig,ax=plt.subplots(figsize=(20,8))
rects1=ax.bar(x-w,very_interested,w,label='Very interested',color='#5cb85c')
rects2=ax.bar(x,somewhat_interested,w,label='Somewhat interested',color='#5bc0de')
rects3=ax.bar(x+w,not_interested,w,label='Not interested',color='#d9534f')

ax.set_ylabel('Percentage',fontsize=14)
ax.set_title("The percentage of the respondents' interest in the different data science Area",
            fontsize=16)
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend(fontsize=14)

def autolabel(rects):
    """Attach a text label above each bar …
Run Code Online (Sandbox Code Playgroud)

python matplotlib bar-chart pandas

6
推荐指数
1
解决办法
1万
查看次数

如何注释条形图并添加自定义图例

我正在尝试绘制一个如下所示的条形图,我不确定如何在每列顶部设置百分比值,并在右侧设置图例。我的代码片段如下。它正在工作,但是缺少百分比值和图例

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

objects = ('18-25', '26-30', '31-40', '40-50')
y_pos = np.arange(len(objects))
performance = [13, 18, 16, 3]
width = 0.35  # the width of the bars
plt.bar(y_pos, performance, align='center', alpha=0.5, color=('red', 'green', 'blue', 'yellow'))
plt.xticks(y_pos, objects)
plt.ylabel('%User', fontsize=16)
plt.title('Age of Respondents', fontsize=20)
width = 0.35

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

python matplotlib bar-chart

1
推荐指数
1
解决办法
7393
查看次数