简体   繁体   English

如何在 plot 和颜色映射表之间设置空间(python)

[英]How to set space between plot and colormap table (python)

I am using secondary y-axis and cmap color but when I plot together the color bar cross to my plot我正在使用辅助 y 轴和 cmap 颜色,但是当我将 plot 一起使用时,颜色条交叉到我的 plot

here is my code这是我的代码

fig,ax1=plt.subplots()


ax1 = df_Combine.plot.scatter('Parameter2', 'NPV (MM €)', marker='s', s=500,   ylim=(-10,60),  c='Lifetime1 (a)', colormap='jet_r', vmin=0, vmax=25, ax=ax1)
graph.axhline(0, color='k')
plt.xticks(rotation=90)


ax2 = ax1.twinx()
ax2.plot(df_Combine_min_select1["CumEnergy1 (kWH)"])


plt.show()

and here is my plotting这是我的阴谋

在此处输入图像描述

anyone can help how to solve this issue?任何人都可以帮助如何解决这个问题?

Thank you谢谢

When you let pandas automatically create a colorbar, you don't have positioning options.当您让 pandas 自动创建颜色条时,您没有定位选项。 Therefore, you can create the colorbar in a separate step and provide the pad= parameter to set a wider gap.因此,您可以在单独的步骤中创建颜色条并提供pad=参数来设置更宽的间隙。 Default, pad is 0.05 , meaning 5% of the width of the subplot.默认情况下, pad0.05 ,表示子图宽度的 5%。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()

df_Combine = pd.DataFrame({'Parameter2': np.random.rand(10) * 10,
                           'NPV (MM €)': np.random.rand(10),
                           'Lifetime1 (a)': np.random.rand(10) * 25,
                           })
ax1 = df_Combine.plot.scatter('Parameter2', 'NPV (MM €)', marker='s', s=500, ylim=(-10, 60), c='Lifetime1 (a)',
                              colormap='jet_r', vmin=0, vmax=25, ax=ax1, colorbar=False)
plt.colorbar(ax1.collections[0], ax=ax1, pad=0.1)

ax2 = ax1.twinx()
ax2.plot(np.random.rand(10))

plt.show()

示例图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM