我想創建一個具有這些特性的條形圖:
- 條與條之間沒有空間
- 組合條形公共值的唯一值
沒有類似的問題!
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
langs = [1,2,3,4,5,6,7,8,9,10]
students = [10,10,20,20,20,20,30,30,15,15]
ax.bar(langs,students)
plt.savefig('barplot.pdf')

ax.bar(langs,students,width = 1.0)
width = 1.0不完全作業。差距也很小。
我想在下面創建確切的輸出。

我怎樣才能做到這一點?
uj5u.com熱心網友回復:
無縫鋼筋
使用width=1并將兩者設定color為ec相同的顏色:
fig, ax = plt.subplots()
width = 1
color = 'blue'
ax.bar(langs, students, width=width, color=color, ec=color)
ax.set_ylim(0, 35)
合并標簽
將熊貓與
import matplotlib.pyplot as plt
import pandas as pd
fig, ax = plt.subplots()
langs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
students = [10, 10, 20, 20, 20, 20, 30, 30, 15, 15, 20, 20, 20]
# ^ another block of 20s
width = 1
color = 'blue'
ax.bar(langs, students, width=width, color=color, ec=color)
ax.set_ylim(0, 35)
df = pd.DataFrame(dict(langs=langs, students=students))
blocks = df.students.ne(df.students.shift()).cumsum()
labels = df.groupby(blocks, as_index=False)[['langs', 'students']].mean()
# langs students
# 0 1.5 10.0
# 1 4.5 20.0
# 2 7.5 30.0
# 3 9.5 15.0
# 4 12.0 20.0 <- repeated 20, so groupby('students') would not have worked
for x, y in zip(labels['langs'], labels['students']):
ax.text(x, y 1, f'{y:.0f}', ha='center')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/438522.html
標籤:Python 熊猫 matplotlib 条形图
上一篇:使用Python多次提取輪廓
下一篇:僅針對一列中的一個特定值繪制圖形
