我需要重新创建一个类似于下面在Excel中创建的图表.我希望使用matplotlib,但似乎无法找到任何关于如何做这样的图表的例子或参考.我需要根据性能阈值对条形进行着色,并显示阈值.谁能指出我正确的方向?不过,我确实需要能够用Python做到这一点.
如何在条形图中的条形图上方添加值的标签:
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame({'Users': [ 'Bob', 'Jim', 'Ted', 'Jesus', 'James'],
'Score': [10,2,5,6,7],})
df = df.set_index('Users')
df.plot(kind='bar', title='Scores')
plt.show()
Run Code Online (Sandbox Code Playgroud) 我问这个问题,因为我还没有找到一个关于如何注释分组水平Pandas条形图的工作示例.我知道以下两点:
但它们都是关于垂直条形图.即,要么没有水平条形图的解决方案,要么它没有完全工作.
在这个问题上工作了几个星期之后,我终于能够用一个示例代码提出问题,这几乎就是我想要的,而不是100%工作.需要你的帮助才能实现100%的目标.
我们走了,完整的代码在这里上传.结果如下:
你可以看到它几乎正常工作,只是标签没有放在我想要的地方,我不能把它们移到一个更好的地方.此外,由于图表栏的顶部用于显示错误吧,这样我真正想要的是移向y轴的注释文字,很好地排队在y轴的左边或右边,这取决于X -值.例如,这是我的同事可以用MS Excel做的事情:
Python可以用Pandas图表做到这一点吗?
我在上面的url中包含了注释的代码,一个是我的全部 - 我可以做的,另一个是参考(来自In [23]
):
# my all-that-I-can-do
def autolabel(rects):
#if height constant: hbars, vbars otherwise
if (np.diff([plt.getp(item, 'width') for item in rects])==0).all():
x_pos = [rect.get_x() + rect.get_width()/2. for rect in rects]
y_pos = [rect.get_y() + 1.05*rect.get_height() for rect in rects]
scores = [plt.getp(item, 'height') for item in rects]
else:
x_pos = [rect.get_width()+.3 for rect in rects]
y_pos = [rect.get_y()+.3*rect.get_height() for rect in …
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像的数据框:
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)
我得到的输出为:
旅游 = 旅游名称
开始 = 开始时的可用预订
结束 = 剩余预订量
.csv 文件列:
ID | Tour | Start | End
12345 | Italy | 100 | 80
13579 | China | 50 | 30
24680 | France | 50 | 30
Run Code Online (Sandbox Code Playgroud)
到目前为止我有这个
import pandas as pd
df = pd.read_csv("items4.csv",sep=",").set_index('ID')
d = dict(zip(df.index,df.values.tolist()))
print(d)
{12345: ['Italy', 100, 80], 13579: ['China', 50, 30], 24680: ['France', 50, 30]} #This is the output
Run Code Online (Sandbox Code Playgroud)
我想用给定的数据制作一个看起来像这样的条形图。
我在此数据集上绘制条形图时遇到麻烦。
+------+------------+--------+
| Year | Discipline | Takers |
+------+------------+--------+
| 2010 | BSCS | 213 |
| 2010 | BSIS | 612 |
| 2010 | BSIT | 796 |
| 2011 | BSCS | 567 |
| 2011 | BSIS | 768 |
| 2011 | BSIT | 504 |
| 2012 | BSCS | 549 |
| 2012 | BSIS | 595 |
| 2012 | BSIT | 586 |
+------+------------+--------+
Run Code Online (Sandbox Code Playgroud)
我正在尝试绘制一个条形图,该条形图用3条形表示每年的接受者数量。这是我做的算法。
import matplotlib.pyplot as plt
import …
Run Code Online (Sandbox Code Playgroud) 我需要帮助在从数据框中的交叉表创建的 pandas 中的堆积条形图的每个部分中添加总数的百分比分布(无小数)。
这是示例数据:
data = {
'Name':['Alisa','Bobby','Bobby','Alisa','Bobby','Alisa',
'Alisa','Bobby','Bobby','Alisa','Bobby','Alisa'],
'Exam':['Semester 1','Semester 1','Semester 1','Semester 1','Semester 1','Semester 1',
'Semester 2','Semester 2','Semester 2','Semester 2','Semester 2','Semester 2'],
'Subject':['Mathematics','Mathematics','English','English','Science','Science',
'Mathematics','Mathematics','English','English','Science','Science'],
'Result':['Pass','Pass','Fail','Pass','Fail','Pass','Pass','Fail','Fail','Pass','Pass','Fail']}
df = pd.DataFrame(data)
# display(df)
Name Exam Subject Result
0 Alisa Semester 1 Mathematics Pass
1 Bobby Semester 1 Mathematics Pass
2 Bobby Semester 1 English Fail
3 Alisa Semester 1 English Pass
4 Bobby Semester 1 Science Fail
5 Alisa Semester 1 Science Pass
6 Alisa Semester 2 Mathematics Pass
7 Bobby Semester …
Run Code Online (Sandbox Code Playgroud) 在过去的 3 天里,我一直试图将每个条形的百分比值放在图上,但没有用!!
df_ds.sort_values('Very interested', ascending = False)
df_dsp = ((100*df_ds / 2233).round(2).astype(str) + '%')
#PLOTTING
ax1 = df_ds.plot(kind = 'bar',
figsize = (20,8),
width = 0.8,
color = ('#5cb85c', '#5bc0de', '#d9534f'),
fontsize = 14)
ax1.set_title("Percentage of Respondents' Interest in Data Science", fontsize = 16)
ax1.legend(fontsize = 14)
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['left'].set_visible(False)
Run Code Online (Sandbox Code Playgroud) 我有两列,我使用 groupby 选项创建了一个名为 output_duration_per_device 的 df,例如
output_duration_per_device=s3_dataset.groupby('DeviceType')['Output_media_duration'].sum().reset_index(name ='format_duration')
output_duration_per_device
DeviceType format_duration
0 Alchemist 8.166905e+06
1 CaptionMaker 1.310864e+07
2 Elemental 1.818089e+07
3 EncodingCloud 0.000000e+00
4 FfMpeg 5.258470e+07
5 FlipFactory 4.747456e+02
6 Rhozet 6.263442e+08
7 Tachyon 4.827463e+06
Run Code Online (Sandbox Code Playgroud)
我可以进行酒吧聊天并找到这样的
output_duration_per_device=s3_dataset.groupby('DeviceType')['Output_media_duration'].sum().reset_index(name ='Device_duration').plot(kind ='bar', figsize=(10,7), fontsize=13)
output_duration_per_device.set_alpha(0.8)
output_duration_per_device.set_title('DeviceType Output Media duration')
output_duration_per_device.set_xlabel('DeviceType')
plt.ylabel('Output_media_duration')
Run Code Online (Sandbox Code Playgroud)
请帮我
我一直在尝试绘制每个条形图上带有值标签的条形图。我已经到处搜索但无法完成此操作。我的 df 是下面这个。
Pillar %
Exercise 19.4
Meaningful Activity 19.4
Sleep 7.7
Nutrition 22.9
Community 16.2
Stress Management 23.9
Run Code Online (Sandbox Code Playgroud)
到目前为止我的代码是
df_plot.plot(x ='Pillar', y='%', kind = 'bar')
plt.show()
Run Code Online (Sandbox Code Playgroud) python ×10
pandas ×8
matplotlib ×6
plot ×3
bar-chart ×2
graph ×1
label ×1
python-3.x ×1
seaborn ×1