简体   繁体   English

使用 Matplotlib 在两个子图中传播数据

[英]Spreading data across two subplots with Matplotlib

I am trying to create two subplots that display a large amount of data from a dataframe.我正在尝试创建两个子图,显示来自 dataframe 的大量数据。 This is just one x axis but results in many column and due to this it does not display correctly over one plot.这只是一个 x 轴,但会导致多列,因此它无法在一个 plot 上正确显示。 I therefore am trying to get one subplot displaying the first 25 columns and the 2nd subplot the next 25 for example.因此,我试图让一个子图显示前 25 列,第二个子图显示接下来的 25 列。 I can create one plot fine, I can create two identical plots fine but cant do what I am trying to achieve.我可以创建一个 plot 很好,我可以很好地创建两个相同的图,但不能做我想要实现的。

I have tried a number of different ways such as this:我尝试了许多不同的方法,例如:

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
plt.subplot(2, 1, 1)
if score <= 100000:
    ax1.plot(score)
else:
    ax2.plot(score)

The above may be completely wrong but when I try this I get the following error:以上可能是完全错误的,但是当我尝试这个时,我得到以下错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I feel like there should be an easy way to do this but I just cant figure it out.我觉得应该有一个简单的方法来做到这一点,但我就是想不通。

You can simply slice your pandas ' 'DataFrame'-object (remember that indices need to be of type integer => cast it):您可以简单地对pandas ' 'DataFrame'-object 进行切片(请记住,索引的类型必须为 integer => 转换):

df[0:int(len(df)/2)]

so you get:所以你得到:

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
# create dummy data
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=['Col 1','Col 2','Col 4','Col 5'])
# open figure
fig, axs = plt.subplots(1, 2, sharey=True)
#plot 1st half [0:50]
axs[0].plot( df[0:int(len(df)/2)] )
#plot 2nd half [50:100]
axs[1].plot( df[int(len(df)/2): ] )

在此处输入图像描述

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

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