在Python中,可以使用matplotlib
库来按条件绘制多个正/负条形图。以下是一个示例代码,展示了如何根据特定条件绘制正负条形图:
import matplotlib.pyplot as plt
import numpy as np
# 示例数据
categories = ['A', 'B', 'C', 'D']
values_positive = [3, 5, 7, 2] # 正值数据
values_negative = [-1, -4, -6, -3] # 负值数据
# 设置条形图的位置
bar_width = 0.35
index = np.arange(len(categories))
# 绘制正条形图
plt.bar(index, values_positive, bar_width, label='Positive', color='green')
# 绘制负条形图
plt.bar(index, values_negative, bar_width, label='Negative', color='red', bottom=values_positive)
# 添加图表元素
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Positive and Negative Bar Charts by Condition')
plt.xticks(index, categories)
plt.legend()
# 显示图表
plt.show()
bottom
参数来解决。通过上述方法和示例代码,可以有效地按条件绘制多个正/负条形图,帮助更好地分析和展示数据。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云