如何基于相同的前一个创建两个不同的图形?

米格尔·冈萨雷斯(Miguel Gonzalez)

我正在尝试根据相同的前一个创建两个不同的图

上一个(fig)包含两个图形共有的一行,从中创建两个新图形(fig1fig2),每个图形都有不同的数据(df1df2)。

这是我想要获得的:

我尝试使用fig.add_subplot函数,但是不断出现错误:ValueError: The Subplot must have been created in the present figure

我创建了一个例子来说明我的意思。执行时会显示值错误”

import pandas as pd
import matplotlib.pyplot as plt

# Data for the two different figures
df1 = pd.DataFrame({'x':np.random.rand(80), 'y':np.random.rand(80)})
df2 = pd.DataFrame({'x':np.random.rand(10), 'y':np.random.rand(10)})


fig, ax = plt.subplots()

# Line creation for both figures
ax.plot(([1,2]))

# Try of creating the two different figures from the previous one:
fig1 = fig.add_subplot(df1.plot(x = 'x', y = 'y', kind = 'scatter'))
fig2 = fig.add_subplot(df2.plot(x = 'x', y = 'y', kind = 'scatter'))

In this example would be very easy to create the line inside of each figure, but that could not be done in the case that I'm working at.

meTchaikovsky

You can pass ax as an argument for df.plot

import pandas as pd
import matplotlib.pyplot as plt

# Data for the two different figures
df1 = pd.DataFrame({'x':np.random.rand(80), 'y':np.random.rand(80)})
df2 = pd.DataFrame({'x':np.random.rand(10), 'y':np.random.rand(10)})


fig, ax = plt.subplots(nrows=1,ncols=2)

# Line creation for both figures
ax[0].plot(([1,2]))
ax[1].plot(([1,2]))

# Try of creating the two different figures from the previous one:
df1.plot(x = 'x', y = 'y', kind = 'scatter', c='violet',ax=ax[0])
df2.plot(x = 'x', y = 'y', kind = 'scatter', c='navy',ax=ax[1])

ax[0].set_title('one')
ax[1].set_title('two')

the output figure is

output


UPDATE

The ax is an array of Axes objects. Different Axes objects are independent, they can have different labels, legends, ticks, etc. If you really need two figures for two plots

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

# Data for the two different figures
df1 = pd.DataFrame({'x':np.random.rand(80), 'y':np.random.rand(80)})
df2 = pd.DataFrame({'x':np.random.rand(10), 'y':np.random.rand(10)})

# fig 1 
fig_0 = plt.figure(0)
ax_0 = fig_0.add_subplot(111)
ax_0.plot(([1,2]))
df1.plot(x = 'x', y = 'y', kind = 'scatter', c='violet',ax=ax_0)
ax_0.set_title('one')

fig_1 = plt.figure(1)
ax_1 = fig_1.add_subplot(111)
ax_1.plot(([1,2]))
df2.plot(x = 'x', y = 'y', kind = 'scatter', c='navy',ax=ax_1)
ax_1.set_title('two')

fig_0.savefig('one.png')
fig_1.savefig('two.png')

You will see from the two saved files, the two plots are in two different figures.

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Plotly-Dash:如何基于两个不同的数据框创建图形?

如何创建一个由两个具有不同内容的相同表组成的表?

基于两个不同的数组创建一个二维数组

如何使用两个相同的变量和不同的值查找一个ID

如何创建基于另外两个表过滤一个表的 MsSQL 过程?

如何创建两个扩展不同图像的相同容器

如何创建一个接受两个相同类型的对象,一个属性并比较值的方法

如何创建在两个不同类中工作的相同 HashMap,而不必在 Android Studio 中创建另一个 HashMap?

如何合并两个不同的表,然后使用vba创建一个新表?

如何在两个不同的网络之间创建一个子网

如何创建一个由两个不同表组成的表

如何在同一个母版上创建两个不同的框架

如何从两个不同的 numpy 数组创建一个 numpy 数组?

如何创建一个C ++类,以基于不同的相似类执行相同的计算?

创建一个函数,该函数接受具有相同方法的两个不同对象

如何基于两个数组创建一个数组

Pandas Dataframe:创建一个新列,并基于两个不同的列在匹配的行中插入一个值

如何基于API级别从Swift中的两个不同类派生一个类?

如何基于一个公共列但内容不同来合并/扩展两个python pandas数据框?

一个页面上不能创建两个以上的c3js图形

如何基于UNIX中的一个匹配列合并两个csv文件。两个文件中列的位置都不同

Maven:如何在两个不同的模块下放置一个具有相同名称的模块?

如何在相同模型之间为两个不同的关联设置一个具有多次通过的

如何使包含一个数字不同部分的两个变量显示为相同的数字?

如何通过DI注入两个相同类型但具有一个属性不同的对象?

即使一个图像的裁剪/比率略有不同,我如何检测到两个图像“相同”?

散景-如何在两个不同的标签中使用相同的小部件(或复制一个小部件)?

如何在一个表中比较两个具有相同值但效果不同的字段?

如何基于另外两个 List<T> 创建一个新的 List<T> 并考虑重复项?