当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Plotting phase changes in matplotlib/pandas

问题描述:

I'd like to be able to use matplotlib & pandas to create graphs based on dynamic data from a google spreadsheet. I've been successful in querying the spreadsheet with gspread, saving to csv, and plotting, but haven't been able to come up with a workaround for phase change lines. Here is an example of format: http://www.ebbp.org/course_outlines/critical_appraisal/pdfs/SSRD-1.gif The phase change lines would need to fall between x-axis values, whether the scale is in dates or sessions. Data paths also cannot connect across the phase change lines.

Here is the code I'm currently working with

 import gspread

import csv

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import getpass

usernm = raw_input("Enter Username")

pw = getpass.getpass("Enter password for user %s: " % usernm)

gc = gspread.login(usernm, pw)

client = raw_input("Which client profile would you like to see?")

wks = gc.open(client)

for i, worksheet in enumerate(wks.worksheets()):

filename = client + '-worksheet' + str(i) + '.csv'

with open(filename, 'wb') as f:

writer = csv.writer(f)

writer.writerows(worksheet.get_all_values())

df = pd.read_csv('CSV FILE HERE', sep=',')

df.plot(x='Date', grid=False, marker='o', color='k', title='Hello')

plt.show()

I moved from using google's visualization API to python and matplotlib because I thought it would have more functionality and features to accomplish this, but there's no way I can think of. Any ideas?

网友答案:

For anyone looking to do this in the future here's the thread I found to be helpful:vertical & horizontal lines in matplotlib

Here is a workable solution:

plt.axvline(x=2.5, ymin=0, ymax = 1.0, linewidth=1, color='k')

Still trying to figure out how to not have data paths connect across (other than interpolate nulls)

网友答案:

Addressing your last comment in your answer, let's say that you have the following changes of phase

changes = [20,35,55]

you can plot the unconnected parts like this

first = 0
for c in changes:
    plt.plot(x[first:c], y[first:c])
    first = c+1
plt.plot(x[first:],y[first:])
分享给朋友:
您可能感兴趣的文章:
随机阅读: