Plotting Multiple Lines in iPython/pandas Produces Multiple Plots

2024/10/5 21:24:36

I am trying to get my head around matplotlib's state machine model, but I am running into an error when trying to plot multiple lines on a single plot. From what I understand, the following code should produce a single plot with two lines:

import pandas as pd
import pandas.io.data as web
aapl = web.get_data_yahoo('AAPL', '1/1/2005')# extract adjusted close
adj_close = aapl.loc[:, ['Adj Close']]# 2 lines on one plot
hold(False)
adj_close.resample('M', how='min').plot()
adj_close.resample('M', how='max').plot()

In fact, I get three figures: first a blank one, and then two with one line each.

blank plot first plot second plot

Any idea what I am doing wrong or what setting on my system might be misconfigured?

Answer

You can pre-create an axis object using matplotlibs pyplot package and then append the plots to this axis object:

import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as pltaapl = web.get_data_yahoo('AAPL', '1/1/2005')# extract adjusted close
adj_close = aapl.loc[:, ['Adj Close']]# 2 lines on one plot
#hold(False)fig, ax = plt.subplots(1, 1)
adj_close.resample('M', how='min').plot(ax=ax)
adj_close.resample('M', how='max').plot(ax=ax)

Plot example

Alternatively, you could concat the two series into a m x 2 DataFrame and plot the dataframe instead:

s1 = adj_close.resample('M', how='min')
s2 = adj_close.resample('M', how='max')
df = pd.concat([s1, s2], axis=1)
df.plot()
https://en.xdnf.cn/q/70441.html

Related Q&A

libclang: add compiler system include path (Python in Windows)

Following this question and Andrews suggestions, I am trying to have liblang add the compiler system include paths (in Windows) in order for my Python codeimport clang.cindexdef parse_decl(node):refere…

Pako not able to deflate gzip files generated in python

Im generating gzip files from python using the following code: (using python 3)file = gzip.open(output.json.gzip, wb)dataToWrite = json.dumps(data).encode(utf-8)file.write(dataToWrite)file.close()Howev…

Best way to have a python script copy itself?

I am using python for scientific applications. I run simulations with various parameters, my script outputs the data to an appropriate directory for that parameter set. Later I use that data. However s…

How to convert dictionary to matrix in python?

I have a dictionary like this:{device1 : (news1, news2, ...), device2 : (news 2, news 4, ...)...}How to convert them into a 2-D 0-1 matrix in python? Looks like this:news1 news2 news3 news4 device1 …

Ubuntu 16.04 - Why I cannot install libtiff4-dev?

Following this tutorial, I am trying to install the OpenCV 3 with Python on Ubuntu 16.04.At the step of entering $ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-devI got this me…

Histogram update in a for loop with matplotlib.pylab

I am trying to update in for loop a histogram data. but I dont know how to make it. I tried with set_data but it is not working. here is the code:plt.ion() ax=plt.subplot(111) [n,X, V]=ax.hist(range(MA…

How to remove a section from an ini file using Python ConfigParser?

I am attempting to remove a [section] from an ini file using Pythons ConfigParser library.>>> import os >>> import ConfigParser >>> os.system("cat a.ini") [a] b = c…

why does this script not work with threading python

so ive been trying to ifnd a way to access task manager. Ive tried a few methods including the wmi module and the windows tasklist but neither suit my need. wmi is way too slow and tasklist becomes to…

django-admin command not working in Mac OS

I started Django in Mac OS and after installing Django using pip, I tried to initiated a new project using the command django-admin startproject mysite. I get the error -bash: django-admin: command not…

HeartBleed python test script

I came across this Python script that tests the server for the HeartBleed vulnerability: Would someone be able to explain the content of the "hello", what is being sent and how was this cont…