Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使x_标签显示在彼此相连的子批次上?_Python_Matplotlib - Fatal编程技术网

Python 如何使x_标签显示在彼此相连的子批次上?

Python 如何使x_标签显示在彼此相连的子批次上?,python,matplotlib,Python,Matplotlib,我正在学习如何创建一个仪表板来跟踪我的开支。第一个目标是创建一个简单的条形图,跟踪我在两个最大类别的收入和支出。下面提供了条形图的代码 income = 5000 immediate_obligations = -1000 true_expenses = -2000 total = income - immediate_obligations - true_expenses fig,ax = plt.subplots() ax.bar(x=[1],height=[income],color =

我正在学习如何创建一个仪表板来跟踪我的开支。第一个目标是创建一个简单的条形图,跟踪我在两个最大类别的收入和支出。下面提供了条形图的代码

income = 5000
immediate_obligations = -1000
true_expenses = -2000
total = income - immediate_obligations - true_expenses

fig,ax = plt.subplots()
ax.bar(x=[1],height=[income],color = 'green',tick_label="Income")
ax.bar(x=[2,3],height=[immediate_obligations,true_expenses], color = 'red', tick_label=["Immediate Obligations","true_expenses"])
ax.bar(x=[4],height=[total], color = 'blue')

fig.suptitle('Spending Current Month')
我之所以选择三个轴,是为了能够将收入标为绿色,支出标为红色,差额标为蓝色。这些绘图渲染得很好,因为它们不重叠。但是,勾号标签仅显示最新创建的绘图。这是有道理的,但如何将标签应用于整个绘图


参数
color
。您可以传递一个数组,该数组的颜色为每个条形图。因此,只需一次调用
bar()
,即可实现输出:

income = 5000
immediate_obligations = -1000
true_expenses = -2000
total = income - immediate_obligations - true_expenses

bars = [income, immediate_obligations, true_expenses, total]
colors = ['g','r','r','b']
labels = ['Income','Immediate obligations','True expenses','Total']

fig,ax = plt.subplots()
ax.bar(x=range(len(bars)), height=bars, color=colors, tick_label=labels)
fig.suptitle('Spending Current Month')