Fixed y axis in Python plotting times in 12 hr format - python

I have this plot but I need the y axis to be fixed to 00:00, 01:00, 02:00, etc all the way up to 12:00. As of now it's only plotting the values I have in the csv on the y axis. the csv is in the following format. How do o get the y axis to be constant and only show 00:00 to 12:00 in 1 hr increments and still have the data plotted correctly?
ML INT 0.1 534.15 0:00
ML EXT 0.25 654.23 3:00
ML INT 0.35 743.12 6:30
And the following is the code I have so far.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('data.csv', header=None)
ints = data[data[1]=='INT']
exts = data[data[1]=='EXT']
INT_index = data[data[1]=='INT'].index
EXT_index = data[data[1]=='EXT'].index
time = [t for t in data[4]]
int_dist = [d for d in ints[3]]
ext_dist = [d for d in exts[3]]
fig, ax = plt.subplots()
ax.scatter(int_dist, INT_index, c='orange', s=150)
ax.scatter(ext_dist, EXT_index, c='black', s=150)
ax.set_yticks(np.arange(len(data[4])))
ax.set_yticklabels(time)
plt.legend(['INT', 'EXT'], loc=4)
plt.xlabel('Distance')
plt.ylabel('Time')
plt.show()

I generated a few more rows of data to make the problem, at least on my end, a bit more meaningful.
What solved this for me was generating a 5th column (in code, not the csv) which is the number of minutes corresponding to a particular o'clock time, i.e. 11:59 maps to 719 min. Using pandas I inserted this new column into the dataframe. I could then place string ticklabels for every hour ('0:00', '1:00', etc.) at every 60 min.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('Workbook2.csv', header=None)
print data
Prints my faked data:
0 1 2 3 4
0 ML INT 0.10 534.15 0:00
1 ML EXT 0.25 654.23 3:00
2 ML INT 0.30 743.12 6:30
3 ML EXT 0.35 744.20 4:30
4 ML INT 0.45 811.47 7:00
5 ML EXT 0.55 777.90 5:45
6 ML INT 0.66 854.70 7:54
7 ML EXT 0.74 798.40 6:55
8 ML INT 0.87 947.30 11:59
Now make a function to convert o'clock to minutes:
def convert_to_min(o_clock):
h, m = o_clock.split(':')
return int(h) * 60 + int(m)
# using this function create a list times in minutes for each time in col 4
min_col = [convert_to_min(t) for t in data[4]]
data[5] = min_col # inserts this list as a new column '5'
print data
Our new data:
0 1 2 3 4 5
0 ML INT 0.10 534.15 0:00 0
1 ML EXT 0.25 654.23 3:00 180
2 ML INT 0.30 743.12 6:30 390
3 ML EXT 0.35 744.20 4:30 270
4 ML INT 0.45 811.47 7:00 420
5 ML EXT 0.55 777.90 5:45 345
6 ML INT 0.66 854.70 7:54 474
7 ML EXT 0.74 798.40 6:55 415
8 ML INT 0.87 947.30 11:59 719
Now build the x and y axis data, the ticklabels, and the tick locations:
INTs = data[data[1]=='INT']
EXTs = data[data[1]=='EXT']
int_dist = INTs[3] # x-axis data for INT
ext_dist = EXTs[3]
# plotting time as minutes in range [0 720]
int_time = INTs[5] # y-axis data for INT
ext_time = EXTs[5]
time = ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00',
'6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00']
# this will place the strings above at every 60 min
tick_location = [t*60 for t in range(13)]
Now plot:
fig, ax = plt.subplots()
ax.scatter(int_dist, int_time, c='orange', s=150)
ax.scatter(ext_dist, ext_time, c='black', s=150)
ax.set_yticks(tick_location)
ax.set_yticklabels(time)
plt.legend(['INT', 'EXT'], loc=4)
plt.xlabel('Distance')
plt.ylabel('Time')
plt.title('Seems to work...')
plt.show()

The ticks will be a lot smarter if you use a datetime for the y-axis.
Fake data:
df = pd.DataFrame({'value':[530,640,710], 'time':['0:00', '3:00', '6:30']})
time value
0 0:00 530
1 3:00 640
2 6:30 710
Convert df.time from str to datetime:
time2 = pd.to_datetime(df.time, format='%H:%M')
plt.plot(df.value, time2, marker='o', linestyle='None')
Can't seem to get this into a scatter instead of plot in case it matters for you (I suppressed the line). Maybe because datetime should always be in a timeseries lineplot and never in a scatterplot (I welcome comments that let me know if this is indeed the case and datetime cannot be put into a scatter).

Related

plot dataframe : Use row as X axe and every line in Y axe

I'm not very confident with python, most of the time I get a code on the web (frequently here). I have a dataframe that I read in panda and I just can't plot it in the way I want (I can't plot it at all, by the way).
Here a sample of my df :
date 00:00 00:30 01:00 01:30 02:00 02:30
16/03/2021 0.518 0.679 0.516 0.633 0.606 0.775
17/03/2021 0.751 0.956 0.823 0.975 1.5 0.73
18/03/2021 0.925 0.733 0.825 1.489 0.762 0.686
19/03/2021 0.834 0.726 0.887 0.712 0.769 0.713
20/03/2021 0.735 0.799 0.732 0.803 0.629 0.811
21/03/2021 0.61 0.425 0.645 0.518 0.451 0.629
22/03/2021 0.401 0.525 0.472 0.518 0.508 0.432
As you see, the very first column is the dates and the very first row is hours of these dates (by step of 30 minutes). The original file cover all 24 hours and is separate by spaces (" ").
I want to plot a curve by day and, for example, 3 days by plot (and I'll make some subplots, I have already done that before, it should be ok...).
Here the beginning of my code :
import pandas as pd
df = pd.read_csv("file.csv", sep=" ",on_bad_lines='skip',usecols=range(48))
Yeah, that's not so much, I know.
These days, I spent a lot of time on python to learn and I now have a solution for this post that I forgot since. If someone needs this some day, here is a code that creates a dataframe line per line, then plot it on a figure with subplot (but we can easily put all data on the same plot when needed):
import pandas as pd
import matplotlib.pyplot as plt
df_all = pd.read_csv('df_test.csv')
df_all.index = df_all['Unnamed: 0']
df_all.drop(['Unnamed: 0'], axis=1, inplace=True)
df = {}
x = 0
for x in range (6) :
globals()[f"df{x}"] = df_all.iloc[x]
x += 1
fig, ax = plt.subplots((x+1),1,sharex=True,sharey=True,figsize=(18,15), dpi=200)
y = 0
for y in range(x):
ax[y].plot(globals()[f"df{y}"].index, globals()[f"df{y}"].iloc[0:])
y += 1

two DataFrame plot in a single plot matplotlip

I want to plot two DataFrame in a single plot.Though, I have seen similar post but none seems to work out.
First 5 rows of my dataframe looks like this:
df1
name type start stop strand
0 geneA transcript 2000 7764 +
1 geneA exon 2700 5100 +
2 geneA exon 6000 6800 +
3 geneB transcript 9000 12720 -
4 geneB exon 9900 10100 -
df2
P1 P2 P3 P4
0 0.28 0.14 0.19 0.19
1 0.30 0.16 0.17 0.20
2 0.26 0.13 0.20 0.12
3 0.21 0.13 0.25 0.15
4 0.31 0.03 0.24 0.20
I want the plot to look like this:
I tried doing this:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
ax = df1.plot()
df1.plot(ax=ax)
but, the output was not meaningful.
I will appreciate suggestions/solutions on how to achieve this.
Here is a minimal example:
import matplotlib.pyplot as plt
f, axes = plt.subplots(nrows=len(df2.columns)+1, sharex=True, )
# plots for df2 columns
i = 0
for col in df2.columns:
df2[col].plot(ax=axes[i])
axes[i].set_ylim(0, 1.2)
axes[i].set_ylabel(col)
i+=1
## code to plot annotations
# axes[-1].plot(…)
axes[-1].set_xlabel('Genomic position')
# remove space between plots
plt.subplots_adjust(hspace=0)
Here is the full graph:
f, axes = plt.subplots(nrows=len(df2.columns)+1, sharex=True, )
# plots for df2 columns
i = 0
for col in df2.columns:
df2[col].plot(ax=axes[i], color='#505050')
axes[i].set_ylim(0, 1.3)
axes[i].set_ylabel(col)
i+=1
## code to plot annotations
axes[-1].set_xlabel('Genomic position')
axes[-1].set_ylabel('annotations')
axes[-1].set_ylim(-0.5, 1.5)
axes[-1].set_yticks([0, 1])
axes[-1].set_yticklabels(['−', '+'])
for _, r in df1.iterrows():
marker = '|'
lw=1
if r['type'] == 'exon':
marker=None
lw=8
y = 1 if r['strand'] == '+' else 0
axes[-1].plot((r['start'], r['stop']), (y, y),
marker=marker, lw=lw,
solid_capstyle='butt',
color='#505050')
# remove space between plots
plt.subplots_adjust(hspace=0)
You can use subplots for doing so (since it is difficult to understand how the two df should be plotted I've provided a general example)
Import matplotlib.pyplot as plt
fig,axes = plt.subplots(4,1) #4 rows, one column
for ax in axes:
plt.plot(X1,y1 ax =ax) # loop over each subplot and create a plot
plt.plot(X2,y2, ax = ax)

how plot multiples dataframe csv in same plot

I have 4 dataframes in 4 csv. I need to plot timeseries ( Date , mean ) in the same plot.
This is my script :
cc = Series.from_csv('D:/python/means2000_2001.csv' , header=0)
fig = plt.figure()
plt.plot(cc , color='red')
fig.suptitle('test title', fontsize=20)
plt.xlabel('Date', fontsize=15)
plt.ylabel('MEANS ', fontsize=15)
plt.xticks(rotation=90)
The 4 dataframes are like this ( x=Date and y=mean )
Out[307]:
Date
07-28 0.17
08-13 0.18
08-29 0.17
09-14 0.19
09-30 0.19
10-16 0.20
11-01 0.18
11-17 0.22
12-03 0.21
12-19 0.82
01-02 0.59
01-18 0.52
02-03 0.54
02-19 0.53
03-07 0.33
03-23 0.32
04-08 0.31
04-24 0.39
05-10 0.40
05-26 0.40
06-11 0.37
06-27 0.33
07-13 0.29
Name: mean, dtype: float64
when I plot the timeseries i have this graph :
how can i plot all dataframes in the same plot with different colors?
I need something like this :
You can do both:
plot all curves with one singel command, see: plt.plot()
adress each singel curve to plot, see for-loop with plt.fill_between()
if you have 2 DataFrames, say df1 and df2, then use plt.plot() twice:
plt.plot(t,df1); plt.plot(t,df2); plt.show()
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
#--- generate data and DataFrame --
nt = 100
t= np.linspace(0,1,nt)*3*np.pi
y1 = np.sin(t); y2 = np.cos(t); y3 = y1*y2
df = pd.DataFrame({'y1':y1,'y2':y2,'y3':y3 })
#--- graphics ---
plt.style.use('fast')
fig, ax0 = plt.subplots(figsize=(20,4))
plt.plot(t,df, lw=4, alpha=0.6); # plot all curves with 1 command
for j in range(len(df.columns)): # add on: fill_between for each curve
plt.fill_between(t,df.values[:,j],label=df.columns[j],alpha=0.2)
plt.legend(prop={'size':15});plt.grid(axis='y');plt.show()
The answer
You can plot multiple dataframes on a single graph by capturing the Axes object that df.plot returns and then reusing it. Here's an example with two dataframes, df1 and df2:
ax = df1.plot(x='dates', y='vals', label='val 1')
df2.plot(x='dates', y='vals', label='val 2', ax=ax)
plt.show()
Output:
Details
Here's the code I used to generate random example values for df1 and df2:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
def random_dates(start, end, n=10):
if isinstance(start, str): start = pd.to_datetime(start)
if isinstance(end, str): end = pd.to_datetime(end)
start_u = start.value//10**9
end_u = end.value//10**9
return pd.to_datetime(np.random.randint(start_u, end_u, n), unit='s')
# generate two random dfs
df1 = pd.DataFrame({'dates': random_dates('2016-01-01', '2016-12-31'), 'vals': np.random.rand(10)})
df2 = pd.DataFrame({'dates': random_dates('2016-01-01', '2016-12-31'), 'vals': np.random.rand(10)})

How to build bar graph or line graph for weekly data in pandas

I want to build the week on the x-axis and the count on the Y-axis for both line and bar graph.
MY data in data frame.
Date----------- count
06-01-2017 18.51 1
06-01-2017 19.11 10
20-01-2017 19.55 20
21-01-2017 20.10 30
22-01-2017 20.10 40
23-01-2017 20.10 50
23-01-2017 20.10 60
29-01-2017 21.33 70
Please advice
df = data.groupby([ pd.Grouper(key='date', freq='W-MON')])['value'].sum().reset_index().sort_values('date')
x= df['date']
y = df['value']
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y, color='skyblue')
ax1.set_xticklabels(x,rotation=90)
plt.show()
this is giving me graph for month wise i need for week wise

Python 3.4 reading from CSV formats

OK So i have this code in Python that Im importing from a csv file the problem is that there are columns in that csv file that aren't basic numbers. There is one column that is text in the format "INT, EXT" and there is a column that is in o'clock format from "0:00 to 11:59" format. I have a third column as a normal number distance in "00.00" format.
My question is how do I go about plotting distance vs o'clock and then basing whether one is INT or EXT changing the colors of the dots for the scatterplot.
My first problem is having how to make the program read oclock format. and text formats from a csv.
Any ideas or suggestions? Thanks in advance
Here is a sample of the CSV im trying to import
ML INT .10 534.15 0:00
ML EXT .25 654.23 3:00
ML INT .35 743.12 6:30
I want to plot the 4th column as the x axis and the 5th column as the y axis
I also want to color code the scatter plot dots red or blue depending if one is INT or EXT
Here is a sample of the code i have so far
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
style.use('ggplot')
a,b,c,d = np.loadtxt('numbers.csv',
unpack = True,
delimiter = ',')
plt.scatter(a,b)
plt.title('Charts')
plt.ylabel('Y Axis')
plt.xlabel('X Axis')
plt.show()
Reading in from your example csv using pandas:
import pandas as pd
import matplotlib.pyplot as plt
import datetime
data = pd.read_csv('data.csv', sep='\t', header=None)
print data
prints:
0 1 2 3 4
0 ML INT 0.10 534.15 0:00
1 ML EXT 0.25 654.23 3:00
2 ML INT 0.35 743.12 6:30
Then separate the 'INT' from the 'EXT':
ints = data[data[1]=='INT']
exts = data[data[1]=='EXT']
change them to datetime and grab the distances:
int_times = [datetime.datetime.time(datetime.datetime.strptime(t, '%H:%M')) for t in ints[4]]
ext_times = [datetime.datetime.time(datetime.datetime.strptime(t, '%H:%M')) for t in exts[4]]
int_dist = [d for d in ints[3]]
ext_dist = [d for d in exts[3]]
then plot a scatter plot for 'INT' and 'EXT' each:
fig, ax = plt.subplots()
ax.scatter(int_dist, int_times, c='orange', s=150)
ax.scatter(ext_dist, ext_times, c='black', s=150)
plt.legend(['INT', 'EXT'], loc=4)
plt.xlabel('Distance')
plt.show()
EDIT: Adding code to answer a question in the comments regarding how to change the time to 12 hour format (ranging from 0:00 to 11:59) and strip the seconds.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('data.csv', header=None)
ints = data[data[1]=='INT']
exts = data[data[1]=='EXT']
INT_index = data[data[1]=='INT'].index
EXT_index = data[data[1]=='EXT'].index
time = [t for t in data[4]]
int_dist = [d for d in ints[3]]
ext_dist = [d for d in exts[3]]
fig, ax = plt.subplots()
ax.scatter(int_dist, INT_index, c='orange', s=150)
ax.scatter(ext_dist, EXT_index, c='black', s=150)
ax.set_yticks(np.arange(len(data[4])))
ax.set_yticklabels(time)
plt.legend(['INT', 'EXT'], loc=4)
plt.xlabel('Distance')
plt.ylabel('Time')
plt.show()
I have worked another answer to this, but will leave the original as I believe it's still good, just not exactly answering your particular question.
I also generated a few more rows of data to make the problem, at least on my end, a bit more meaningful.
What solved this for me was generating a 5th column (in code, not the csv) which is the number of minutes corresponding to a particular o'clock time, i.e. 11:59 maps to 719 min. Using pandas I inserted this new column into the dataframe. I could then place string ticklabels for every hour ('0:00', '1:00', etc.) at every 60 min.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('Workbook2.csv', header=None)
print data
Prints my faked data:
0 1 2 3 4
0 ML INT 0.10 534.15 0:00
1 ML EXT 0.25 654.23 3:00
2 ML INT 0.30 743.12 6:30
3 ML EXT 0.35 744.20 4:30
4 ML INT 0.45 811.47 7:00
5 ML EXT 0.55 777.90 5:45
6 ML INT 0.66 854.70 7:54
7 ML EXT 0.74 798.40 6:55
8 ML INT 0.87 947.30 11:59
Now make a function to convert o'clock to minutes:
def convert_to_min(o_clock):
h, m = o_clock.split(':')
return int(h) * 60 + int(m)
# using this function create a list times in minutes for each time in col 4
min_col = [convert_to_min(t) for t in data[4]]
data[5] = min_col # inserts this list as a new column '5'
print data
Our new data:
0 1 2 3 4 5
0 ML INT 0.10 534.15 0:00 0
1 ML EXT 0.25 654.23 3:00 180
2 ML INT 0.30 743.12 6:30 390
3 ML EXT 0.35 744.20 4:30 270
4 ML INT 0.45 811.47 7:00 420
5 ML EXT 0.55 777.90 5:45 345
6 ML INT 0.66 854.70 7:54 474
7 ML EXT 0.74 798.40 6:55 415
8 ML INT 0.87 947.30 11:59 719
Now build the x and y axis data, the ticklabels, and the tick locations:
INTs = data[data[1]=='INT']
EXTs = data[data[1]=='EXT']
int_dist = INTs[3] # x-axis data for INT
ext_dist = EXTs[3]
# plotting time as minutes in range [0 720]
int_time = INTs[5] # y-axis data for INT
ext_time = EXTs[5]
time = ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00',
'6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00']
# this will place the strings above at every 60 min
tick_location = [t*60 for t in range(13)]
Now plot:
fig, ax = plt.subplots()
ax.scatter(int_dist, int_time, c='orange', s=150)
ax.scatter(ext_dist, ext_time, c='black', s=150)
ax.set_yticks(tick_location)
ax.set_yticklabels(time)
plt.legend(['INT', 'EXT'], loc=4)
plt.xlabel('Distance')
plt.ylabel('Time')
plt.title('Seems to work...')
plt.show()

Categories