python - 带有辅助 y 轴的 Python Matplotlib 多条形图
问题描述
我有一个多栏聊天,我添加了一条带有辅助 y 轴的行,其中包含以下代码:
import matplotlib.pyplot as plt
import numpy as np
labels = ['a','b','c','d','e']
d1 = [1, 1, 1, 1, 1]
d2 = [2, 2, 2, 2, 2]
d3 = [3, 3, 3, 3, 3]
d4 = [400, 600, 800, 1000, 1200]
x = np.arange(len(labels))
width = 0.25
fig, ax = plt.subplots()
rects1 = ax.bar(x + 0, d1, width, color='red')
rects2 = ax.bar(x + 0.25, d2, width, color='blue')
rects3 = ax.bar(x + 0.5, d3, width, color='green')
ax.set_xticks(x+0.25)
ax.set_xticklabels(labels)
ax2 = ax.twinx()
ax2.plot(d4, color='black', marker='*', linewidth=2, markersize=4)
ax2.margins(x=0.02)
plt.show()
图像如下所示:
您可以看到这条线(黑色)不遵循 x 轴(见下文):
如何移动线并使其跟随 x 轴(即,线上的数据点应指向每个 x 标签的中间)。
解决方案
推荐阅读
- azure - 使用函数应用创建数据库导出并将其保存到 Azure 门户中的安全存储帐户
- android - 登录期间应用程序进入后台后未调用 onActivityResult
- xamarin.forms - 如何从内容页面视图模型中隐藏内容视图(Xamarin 表单,freshmvvm)
- ios - 由于 UIWebView API 已弃用,因此无法在 Appstore 上发布。如何正确迁移到 WKWebView?
- javascript - 为什么即使输入数字是 Javascript 中的字符串,年龄计算器也能工作?
- node.js - 如何在节点js中传递对象中的变量值
- f# - ServiceStack F# dotnet core 3.1 示例
- swiftui - SwiftUI - TextField disabled - 如何选择和复制文本?
- ibm-mq - 无法通过 Logic App MQ 连接器访问托管在 Azure vm 中的 IBM MQ
- ios - 如何从 XCode 项目创建“.ipa”?