相关疑难解决方法(0)

python matplotlib条形图添加条形标题

我在 python 2.7 中使用 matplotlib

我需要创建一个简单的 pyplot 条形图,对于每个条形图,我需要在它上面添加它的 y 值。

我正在使用以下代码创建条形图:

import matplotlib.pyplot as plt

barlist = plt.bar([0,1,2,3], [100,200,300,400], width=5)

barlist[0].set_color('r')
barlist[0].title("what?!")
Run Code Online (Sandbox Code Playgroud)

更改颜色有效,但对于标题,我收到以下错误:AttributeError: 'Rectangle' object has no attribute 'title'

我发现了一些问题,有关类似的问题,但他们没有用创建条形图以同样的方式,以及他们的解决方案并没有为我工作。

将条形的值添加为它们上方的标题的简单解决方案的任何想法?

谢谢!

python matplotlib python-2.7

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

在直方图上添加数据标签 Python/Matplotlib

我正在尝试在直方图顶部添加数据标签值,以尝试明显地显示频率。

这是我现在的代码,但不确定如何编码以将值放在上面:

plt.figure(figsize=(15,10))
plt.hist(df['Age'], edgecolor='white', label='d')
plt.xlabel("Age")
plt.ylabel("Number of Patients")
plt.title = ('Age Distrubtion') 
Run Code Online (Sandbox Code Playgroud)

我想知道是否有人知道执行此操作的代码:

在此输入图像描述

python annotations matplotlib histogram bar-chart

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

如何在matplotlib中使用bar_label旋转标签?

  1. 如何使用 将条形图中的标签旋转 90 度matplotlib.bar_label()在此输入图像描述

  2. 在的文档matplotlib.bar_label()中,您能用一些示例解释以下参数的用法吗?

**kwargs:任何剩余的关键字参数都会传递到 Axes.annotate。

#CODE TO PRODUCE ABOVE SHOWN CHART

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')

# Add some text for …
Run Code Online (Sandbox Code Playgroud)

python matplotlib python-3.x seaborn

7
推荐指数
0
解决办法
5181
查看次数

将条形图与热图组合时如何对齐 yticklabel

我有与这个问题类似的问题;我试图在 Seaborn 中合并三个图,但 y 轴上的标签未与条形对齐。

我的代码(现在是一个有效的复制粘贴示例):

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

### Generate example data
np.random.seed(123)
year = [2018, 2019, 2020, 2021]
task = [x + 2 for x in range(18)]
student = [x for x in range(200)]
amount = [x + 10 for x in range(90)]
violation = [letter for letter in "thisisjustsampletextforlabels"] # one letter labels

df_example = pd.DataFrame({

    # some …
Run Code Online (Sandbox Code Playgroud)

python heatmap bar-chart pandas seaborn

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

将数据标签添加到Seaborn因子图

我想为Seaborn生成的因子图添加数据标签.这是一个例子:

import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

titanic_df = pd.read_csv('train.csv')
sns.factorplot('Sex',data=titanic_df,kind='count')
Run Code Online (Sandbox Code Playgroud)

此图像已创建

如何将"计数"值添加到图表上每个条形的顶部?

python matplotlib seaborn

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

在 Matplotlib、Python3 中的极坐标/径向条形图中的条形图顶部获取标签

我想创建一个径向条形图。我有以下 Python3 代码:

lObjectsALLcnts = [1, 1, 1, 2, 2, 3, 5, 14, 15, 20, 32, 33, 51, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 6, 7, 7, 10, 10, 14, 14, 14, 17, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 5, 5, 6, 14, 14, 27, 27, 1, 1, 2, 3, 4, 4, 5]`

lObjectsALLlbls = ['DuctPipe', 'Column', 'Protrusion', 'Tree', 'Pole', 'Bar', 'Undefined', 'EarthingConductor', 'Grooves', 'UtilityPipe', 'Cables', 'RainPipe', 'Moulding', 'Intrusion', 'PowerPlug', 'UtilityBox', 'Balcony', …
Run Code Online (Sandbox Code Playgroud)

python matplotlib bar-chart python-3.x polar-coordinates

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

在 seaborn 条形图上显示计数

我有一个看起来像的数据框:

  User   A       B     C
   ABC   100    121   OPEN
   BCD   200    255   CLOSE
   BCD   500    134   OPEN
   DEF   600    125   CLOSE
   ABC   900    632   OPEN
   ABC   150    875   CLOSE
   DEF   690    146   OPEN
Run Code Online (Sandbox Code Playgroud)

我正在尝试在“用户”列上显示计数图。代码如下:

fig, ax1 = plt.subplots(figsize=(20,10))
graph = sns.countplot(ax=ax1,x='User', data=df)
graph.set_xticklabels(graph.get_xticklabels(),rotation=90)
for p in graph.patches:
    height = p.get_height()
    graph.text(p.get_x()+p.get_width()/2., height + 0.1,
        'Hello',ha="center")
Run Code Online (Sandbox Code Playgroud)

输出看起来像:

在此处输入图片说明

但是,我想用列“用户”的 value_counts 替换字符串“Hello”。当我添加代码以向图形添加标签时:

for p in graph.patches:
    height = p.get_height()
    graph.text(p.get_x()+p.get_width()/2., height + 0.1,
        df['User'].value_counts(),ha="center")
Run Code Online (Sandbox Code Playgroud)

我得到的输出为:

在此处输入图片说明

python matplotlib pandas seaborn

6
推荐指数
3
解决办法
9864
查看次数

Seaborn Catplot 在条形图上设置值

我策划了catplotseaborn这样的

import seaborn as sns
import pandas as pd

data  = {'year': [2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015], 'geo_name': ['Michigan', 'Michigan', 'Michigan', 'Michigan', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Ann Arbor, MI', 'Ann Arbor, MI', 'Ann Arbor, MI', 'Ann Arbor, MI', 'Philadelphia, PA', 'Philadelphia, PA', 'Philadelphia, PA', 'Philadelphia, PA', 'Ann Arbor, MI Metro Area', 'Ann Arbor, …
Run Code Online (Sandbox Code Playgroud)

python plot data-visualization matplotlib seaborn

6
推荐指数
2
解决办法
4985
查看次数

是否可以将数字放在 matplot 直方图上?

import matplotlib.pyplot as plt
import numpy as np

randomnums = np.random.normal(loc=9,scale=6, size=400).astype(int)+15
Run Code Online (Sandbox Code Playgroud)

输出:

array([25, 22, 19, 26, 24,  9, 19, 32, 30, 25, 29, 17, 21, 14, 17, 27, 27,
       28, 17, 17, 20, 21, 16, 28, 20, 24, 15, 20, 20, 13, 33, 21, 30, 27,
        8, 22, 24, 25, 23, 13, 24, 20, 16, 32, 15, 26, 34, 16, 21, 21, 28,
       22, 23, 18, 20, 22, 23, 22, 23, 26, 22, 25, 19, 29, 14, 27, 21, …
Run Code Online (Sandbox Code Playgroud)

python statistics numpy matplotlib histogram

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

在 matplotlib bar_label 中隐藏小于 n 的条形标签

我喜欢最近 matpolotlib 更新中 ax.bar_label 的易用性。

我热衷于隐藏低值数据标签以提高最终图中的可读性,以避免标签重叠。

如何在下面的代码中隐藏小于预定义值(这里假设小于 0.025)的标签?

在此输入图像描述

df_plot = pd.crosstab(df['Yr_Lvl_Cd'], df['Achievement_Cd'], normalize='index')
ax = df_plot.plot(kind = 'bar', stacked = True,  figsize= (10,12))
for c in ax.containers:
    ax.bar_label(c, label_type='center', color = "white")
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib

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