visualising data with python of time series and float colmn - python

i have the following quastion-
What can you tell about the relationship between time and speed? Is there a best time of day to connect? Has it changed throughout the years?
this is my dataframedataframe
my columns
data
does any one have any suggestion on how i would aprouch this question ?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv('/Users/dimagoroh/Desktop/data_vis/big_file.csv', low_memory=False)
sns.lmplot(x="hours",y="speed",data=df)
im trying to do a plot but get this error i think i need to manipulate the hour column to a diffrent data type right now it is set as object

Please post the error you get. From the data I think you need to pass x="hour" and not x="hours". Also try
df.hour = pd.to_datetime(df.hour)

Related

Can't choose a column of a data frame on Python

I'm trying to plot a figure on Python but I get a KeyError. I can't read the column "Cost per Genome" for some reason.
Here is my code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("Sequencing_Cost_Data_Table_Aug2021 - Data Table.csv") #The data can be found here: https://docs.google.com/spreadsheets/d/1auLPEnAp0aI__zIyK9fKBAkLpwQpOFBx9qOWgJoh0xY/edit#gid=729639239
fig = plt.figure()
plt.plot(data["Date"],data["Cost per Genome"])
It looks like either you have interpreted the data wrong into the Dataframe, of made an error with the plot. Read this. It might help you further: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
P.S. I couldn't acces your spreadsheet. It was request only

Plotting Windrose from csv

I would like to plot a windrose from data in .csv file.
From the Windrose documentation, it looks like I need the wind speed, wind direction, and date as index column (csv here).
I tried multiple ways around it but always run into errors.
The error I have now: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Should I just leave out the index column or what would be the best option to plot a windrose from csv?
from windrose import WindroseAxes
from matplotlib import pyplot as plt
import matplotlib.cm as cm
import numpy as np
import pandas as pd
from windrose import plot_windrose
df = pd.read_csv("Wind2.csv",index_col='Date', names = ["Date", "speed", "direction"], sep=";")
ws = df["speed"].values
wd = df["direction"].values
plot_windrose(df, kind='contour', bins=np.arange(0.01,8,1), cmap=cm.hot, lw=3)
plot.show()
You have missing data - for instance, in line 182970, you're missing speed data.
Try manually filtering or filling in the data, or try using pandas' filter function to remove the offending lines.

PyCharm not showing dataframe when I press ctrl+alt+f10

I'm trying to backtest a simple strategy of mine and the first step is to retrieve historical data from yfinance. However, whenver I run this, I can't see the contents of hist. Instead, it just has this outputthis output
# import all the libraries
import nsetools as ns
import pandas as pd
import numpy
from datetime import datetime
import yfinance as yf
import matplotlib.pyplot as plot
plot.style.use('classic')
a = input("Enter the ticker name you wish to apply strategy to")
ticker = yf.Ticker(a)
hist = ticker.history(period="1mo", interval="5m")
hist
I really just want to see the historical prices against the time but can't get the dataframe to appear. I would appreciate any input on this.

Set matplotlib backend from Pandas

I am currently facing the following issue. I have a couple of Python scripts that plot some useful information using the Python module Pandas which uses Matplotlib .
As far as I understand matplotlib let set its backend as described on the accepted answer to this question.
I would like to set the matplotlib backend from Pandas:
Is it possible?
How can I do it?
EDIT 1:
By the way my code looks like:
import pandas as pd
from pandas import DataFrame, Series
class MyPlotter():
def plot_from_file(self, stats_file_name, f_name_out, names,
title='TITLE', x_label='x label', y_label='y label'):
df = pd.read_table(stats_file_name, index_col=0, parse_dates=True,
names= names)
plot = df.plot(lw=2,colormap='jet',marker='.',markersize=10,title=title,figsize=(20, 15))
plot.set_xlabel(x_label)
plot.set_ylabel(y_label)
fig = plot.get_figure()
fig.savefig(f_name_out)
plot.cla()
I've just applied the solution posted on the this question and it worked out.
In others words, my code imports looked as:
import pandas as pd
from pandas import DataFrame, Series
After applying the solution the imports look as:
import pandas as pd
from pandas import DataFrame, Series
import matplotlib
matplotlib.use('pdf')
import matplotlib.pyplot as plt
I know I am answering my own question, but I am doing so in case someone can find it useful.

How to refer/assign an excel column in python?

I have a csv file (excel spreadsheet) of a column of roughly a million numbers in column A. I want to make a histogram of this data with the frequency of the numbers on the y-axis and the number quantities on the x-axis. I'm using pandas to do so. My code:
import pandas as pd
pd.read_csv('D1.csv', quoting=2)['A'].hist(bins=50)
Python isn't interpreting 'A' as the column name. I've tried various names to reference the column, but all result in a keyword error. Am I missing a step where I have to assign that column a name via python which I don't know how to?
I need more rep to comment, so I put this as answer.
You need to have a header row with the names you want to use on pandas. Also if you want to see the histogram when you are working from python shell or ipython you need to import pyplot
import matplotlib.pyplot as plt
import pandas as pd
pd.read_csv('D1.csv', quoting=2)['A'].hist(bins=50)
plt.show()
Okay I finally got something to work with headings, titles, etc.
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('D1.csv', quoting=2)
data.hist(bins=50)
plt.xlim([0,115000])
plt.title("Data")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
My first problem was that matplotlib is necessary to actually show the graph as stated by #Sauruxum. Also, I needed to set the action
pd.read_csv('D1.csv', quoting=2)
to data so I could plot the histogram of that action with
data.hist
Basically, the problem wasn't finding the name to the header row. The action itself needed to be .hist .Thank you all for the help.

Categories

Resources