简体   繁体   中英

Plot Multiple lines in Python from CSV

I am trying to plot the growth of sales on a per-city basis over time. I have a csv file that I'm importing with pandas...

import pandas as pd
import numpy as np
import matplotlib as plt    
df = pd.read_csv("filepath.csv")

In that dataframe are three columns, date (formatted m/d/yyyy 0:00:00), order value, and city.

I'm trying to plot the occurance of orders in each city as a separate line, such that the first date there's an order for a particular city, the y value will be 1, the second date there's an order for that city, the y value will be two, etc.

I also need the code to ignore null values for City.

So far I've come up with this:

style.use('ggplot')

df.groupby('City').plot(x='Date', y='Weekly Payment')

plt.title('Title of Chart')
plt.ylabel('Y Axis')
plt.xlabel('X Axis')

plt.show()

It returns an error saying there is no numeric data to plot. Help much appreciated!

edit: here is an image of the kind of graph I'd like to create:

imgur上的图像

I believe the issue you're having is that you don't have the same ammount of data for all cities,

In order for information to be plotted in the same figure, the data sets need to be the same size for every plot. So what you need to do is generate dummy data to fill in the gaps where data is missing for your second plot.

As for ignoring null cities a simple try and except wrapper should do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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