在 matplotlib 中使用 twinx() 時將子圖數量加倍

大老闆01

這是我的圖表:

在此處輸入圖片說明 不幸的是,這也在那裡,就在下面:

在此處輸入圖片說明

這是代碼:

fig,ax1 = plt.subplots(6,1, figsize=(20,10),dpi=300)
fig2,ax2 = plt.subplots(6,1, figsize=(20,10),dpi=300)

for index, val in enumerate(datedf.columns):
    g = ax1[index].plot(datedf.index, datedf[val], color=colors[index])
    ax1[index].set(ylim=[-100,6500])
    ax2[index] = ax1[index].twinx()
    a = ax2[index].plot(qtydf.index, qtydf[val], color=colors[index], alpha=0.5)
    ax2[index].set(ylim=[200,257000])

我試過這個答案,但我在第一行出錯(解包的值太多)誰能解釋為什麼?

莫茲韋

您生成了 2 個數字,因此最終得到 2 個數字。

相反,您應該執行以下操作:

fig, axes = plt.subplots(6,1, figsize=(20,10),dpi=300)

for index, val in enumerate(datedf.columns):
    ax1 = axes[index]
    g = ax1.plot(datedf.index, datedf[val], color=colors[index])
    ax1.set(ylim=[-100,6500])
    ax2 = ax1.twinx()
    ax2.plot(qtydf.index, qtydf[val], color=colors[index], alpha=0.5)
    ax2.set(ylim=[200,257000])

注意。該代碼未經測試,因為我沒有原始數據集。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

matplotlib:使用多个 twinx 子图控制 y 轴标签的位置

不同比例的 Matplotlib twinx

繪製圖形並使用 seaborn 添加 twinx()

Matplotlib:Draggable不适用于使用twinx()生成的多个图例

Matplotlib:使用Twinx覆盖时,箱线图和条形图发生偏移

Python + Matplotlib-> Twinx出现NoneType错误

麻烦对齐matplotlib twinx轴的刻度

子图和 twinx 轴

Python matplotlib - 將直方圖加倍

Matplotlib:Twinx图隐藏在主轴背景后面

matplotlib:如何获取现有的twinx()轴的句柄?

带有Matplotlib和Twinx的多个Y轴

条形图 Matplotlib:twinx 的日期间隔(xaxis)问题

使用 twinx 时旋转 xtick 标签的问题

在 Django 中使用 Matplotlib

在pygame中使用matplotlib

在matplotlib中使用\ mathbb {}

在 Matplotlib 中使用乳胶

在 twinx 上將 x 軸數值與 seaborn 中的相應分類變量對齊

如何将 matplotlib 轴方面设置为与 twinx() 和 twiny() 相等

Matplotlib轴的自动缩放功能在twinx()之后不起作用

twinx轴的matplotlib axes.clear()不会清除第二个y轴标签

在matplotlib中,应用twinx时pyplot.text消失。它怎么出现?

matplotlib twinx()第2轴标记在同一左轴上

matplotlib:具有超过 4 个 twinx 轴的紧密布局具有额外的填充

如何使用seaborn和twinx在条形图上绘制线图

在Julia中使用Matplotlib的补丁

在绘图中使用 * - matplotlib

如何在 xlim、ylim 更改時使用 matplotlib 繪圖將標記放置在固定位置