python - pandas barplot 为每个变量选择颜色
问题描述
我通常使用 matplotlib,但在玩熊猫绘图时遇到了意想不到的行为。我假设以下将返回红色和绿色边缘而不是交替。我在这里想念什么?
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({"col1":[1,2,4,5,6], "col2":[4,5,1,2,3]})
def amounts(df):
fig, ax = plt.subplots(1,1, figsize=(3,4))
(df.filter(['col1','col2'])
.plot.bar(ax=ax,stacked=True, edgecolor=["red","green"],
fill=False,linewidth=2,rot=0))
ax.set_xlabel("")
plt.tight_layout()
plt.show()
amounts(df)
解决方案
我认为分别绘制每一列并将bottom
参数设置为堆叠条形可提供您想要的输出。
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({"col1":[1,2,4,5,6], "col2":[4,5,1,2,3]})
def amounts(df):
fig, ax = plt.subplots(1,1, figsize=(3,4))
df['col1'].plot.bar(ax=ax, linewidth=2, edgecolor='green', rot=0, fill=False)
df['col2'].plot.bar(ax=ax, bottom=df['col1'], linewidth=2, edgecolor='red', rot=0, fill=False)
plt.legend()
plt.tight_layout()
plt.show()
amounts(df)
推荐阅读
- jquery - jquery数组json推送多维
- javascript - 如何在javascript中将对象添加到数组中的另一个对象
- java - 如何使用 thymeleaf 动态更新表格?
- openshift - Openshift OKD 3.11 安装在任务期间崩溃 [openshift_node : 安装节点、客户端和 conntrack 包]
- arrays - Maxima:定义一个函数,该函数返回一个范围内的随机整数,使得该值与另一个值或其他值的列表不同
- tomcat - 内容安全策略未出现
- typescript - 函数参数返回类型的打字稿类型推断
- prolog - Prolog:将字符列表分配给数字列表
- python - 如何比较两个二维数组以查看它们的第二个元素是否匹配?
- asp.net - 代理错误:无法将请求从 localhost:3000 代理到 http://localhost:44381/ (ECONNREFUSED)