Plot Overlapping Histograms Using Python - python
I have a .csv file (csv_test_1.csv) that is in this format:
durum_before_length,durum_before_reads,durum_after_length,durum_after_reads
0,0,0,0
10,0,10,0
20,0,20,0
30,0,30,1
40,0,40,4
50,0,50,5
60,0,60,0
70,0,70,1
80,0,80,4
90,0,90,1
100,4840,100,4704
110,4817,110,4706
120,4983,120,4860
130,4997,130,4851
140,5142,140,4980
150,5363,150,5192
160,5756,160,5530
170,6054,170,5725
180,6335,180,5989
190,7051,190,6651
200,9003,200,7157
210,8446,210,7812
220,9088,220,8314
230,9761,230,8955
240,10637,240,9660
250,11659,250,10408
260,12572,260,11178
270,13139,270,11538
280,13985,280,11950
290,113552,290,14304
300,954175,300,16383
,,310,17230
,,320,18368
,,330,19158
,,340,19733
,,350,20754
,,360,21698
,,370,21991
,,380,21937
,,390,22473
,,400,22655
,,410,22497
,,420,22460
,,430,22488
,,440,21941
,,450,21884
,,460,21350
,,470,21066
,,480,20812
,,490,19901
,,500,19716
,,510,19374
,,520,19000
,,530,18245
,,540,17220
,,550,15713
,,560,14042
,,570,11932
,,580,7204
,,590,29
You can see that the second two columns are longer than the first two columns. I would like to plot two overlapping histograms: the first histogram will be the first column as the x values plotted against the second column as the y-values, and the second histogram will be the third column as the x values plotted against the fourth column as the y-values.
I am thinking of using seaborn because it makes nice looking plots. The code I have thus far is as shown below. From here, I have no idea how to specify the x and y values and how to generate two overlapping histograms on the same plot. Any advice would be greatly appreciated.
import numpy as np
import pandas as pd
from pandas import read_csv
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
read_data = read_csv("csv_test_1.csv")
sns.set(style="white", palette="muted")
sns.despine()
plt.hist(read_data, normed=False)
plt.xlabel("Read Length")
plt.ylabel("Number of Reads")
Related
How can one create histograms with subplots according to grouped variables in seaborn?
I am attempting to create a histogram using seaborn and census data that displays 3 subplots for age composition, and I have the data grouped the way that I would like it, but I am struggling to turn that into a histogram. import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt filename = "/scratch/%s_class_root/%s_class/materials/data/pums_short.csv.gz" acs = pd.read_csv(filename) R65_agg = acs.groupby(["R65", "PUMA"])["HINCP"] R65_meds = R65_agg.agg(np.median).unstack() R65_f = R65_meds.dropna() R65_f = R65_meds.reset_index(drop = True) I was expecting this code to give me data that I could plug into a histogram but instead of being distinct subplots, the "0.0, 1.0, 2,0" in the final variable just get added together when I apply the .describe() function. Any advice for how I can convert this into a form that's readable with the sns.histplot() function?
Python: How to construct a joyplot with values taken from a column in pandas dataframe as y axis
I have a dataframe df in which the column extracted_day consists of dates ranging between 2022-05-08 to 2022-05-12. I have another column named gas_price, which consists of the price of the gas. I want to construct a joyplot such that for each date, it shows the gas_price in the y axis and has minutes_elapsed_from_start_of_day in the x axis. We may also use ridgeplot or any other plot if this doesn't work. This is the code that I have written, but it doesn't serve my purpose. from joypy import joyplot import matplotlib.pyplot as plt df['extracted_day'] = df['extracted_day'].astype(str) joyplot(df, by = 'extracted_day', column = 'minutes_elapsed_from_start_of_day',figsize=(14,10)) plt.xlabel("Number of minutes elapsed throughout the day") plt.show()
Create dataframe with mock data: import pandas as pd import numpy as np import matplotlib.pyplot as plt from joypy import joyplot np.random.seed(111) df = pd.DataFrame({ 'minutes_elapsed_from_start_of_day': np.tile(np.arange(1440), 5), 'extracted_day': np.repeat(['2022-05-08', '2022-05-09', '2022-05-10','2022-05-11', '2022-05-12'], 1440), 'gas_price': abs(np.cumsum(np.random.randn(1440*5)))}) Then create the joyplot. It is important that you set kind='values', since you do not want joyplot to show KDEs (kernel density estimates, joyplot's default) but the raw gas_price values: joyplot(df, by='extracted_day', column='gas_price', kind='values', x_range=np.arange(1440), figsize=(7,5)) The resulting joyplot looks like this (the fake gas prices are represented by the y-values of the lines):
How to remove certain values before plotting data
I'm using python for the first time. I have a csv file with a few columns of data: location, height, density, day etc... I am plotting height (i_h100) v density (i_cd) and have managed to constrain the height to values below 50 with the code below. I now want to constrain the values on the y axis to be within a certain 'day' range say (85-260). I can't work out how to do this. import pandas import matplotlib.pyplot as plt data=pandas.read_csv('data.csv') data.plot(kind='scatter',x='i_h100',y='i_cd') plt.xlim(right=50)
Use .loc to subset data going into graph. import pandas as pd import numpy as np import matplotlib.pyplot as plt # Make some dummy data np.random.seed(42) df = pd.DataFrame({'a':np.random.randint(0,365,20), 'b':np.random.rand(20), 'c':np.random.rand(20)}) # all data: plot of 'b' vs. 'c' df.plot(kind='scatter', x='b', y='c') plt.show() # use .loc to subset data displayed based on value in 'a' # can also use .loc to restrict values of 'b' displayed rather than plt.xlim df.loc[df['a'].between(85,260) & (df['b'] < 0.5)].plot(kind='scatter', x='b', y='c') plt.show()
Simple Graph Does Not Represent Data
This is a very straightforward question. I have and x axis of years and a y axis of numbers increasing linearly by 100. When plotting this with pandas and matplotlib I am given a graph that does not represent the data whatsoever. I need some help to figure this out because it is such a small amount of code: The CSV is as follows: A,B 2012,100 2013,200 2014,300 2015,400 2016,500 2017,600 2018,700 2012,800 2013,900 2014,1000 2015,1100 2016,1200 2017,1300 2018,1400 The Code: import matplotlib.pyplot as plt import numpy as np import pandas as pd data = pd.read_csv("CSV/DSNY.csv") data.set_index("A", inplace=True) data.plot() plt.show() The graph this yields is: It is clearly very inconsistent with the data - any suggestions?
The default behaviour of matplotlib/pandas is to draw a line between successive data points, and not to mark each data point with a symbol. Fix: change data.plot() to data.plot(style='o'), or df.plot(marker='o', linewidth=0). Result:
All you need is sort A before plotting. import matplotlib.pyplot as plt import numpy as np import pandas as pd data = pd.read_csv("CSV/DSNY.csv").reset_index() data = data.sort_values('A') data.set_index("A", inplace=True) data.plot() plt.show()
Clustermapping in Python using Seaborn
I am trying to create a heatmap with dendrograms on Python using Seaborn and I have a csv file with about 900 rows. I'm importing the file as a pandas dataframe and attempting to plot that but a large number of the rows are not being represented in the heatmap. What am I doing wrong? This is the code I have right now. But the heatmap only represents about 49 rows. Here is an image of the clustermap I've obtained but it is not displaying all of my data. import seaborn as sns import pandas as pd from matplotlib import pyplot as plt # Data set df = pd.read_csv('diff_exp_gene.csv', index_col = 0) # Default plot sns.clustermap(df, cmap = 'RdBu', row_cluster=True, col_cluster=True) plt.show() Thank you.
An alternative approach would be to use imshow in matpltlib. I'm not exactly sure what your question is but I demonstrate a way to graph points on a plane from csv file import numpy as np import matplotlib.pyplot as plt import csv infile = open('diff_exp_gene.csv') df = csv.DictReader(in_file) temp = np.zeros((128,128), dtype = int) for row in data: if row['TYPE'] == types: temp[int(row['Y'])][int(row['X'])] = temp[int(row['Y'])][int(row['X'])] + 1 plt.imshow(temp, cmap = 'hot', origin = 'lower') plt.show()
As far as I know, keywords that apply to seaborn heatmaps also apply to clustermap, as the sns.clustermap passes to the sns.heatmap. In that case, all you need to do in your example is to set yticklabels=True as a keyword argument in sns.clustermap(). That will make all of the 900 rows appear. By default, it is set as "auto" to avoid overlap. The same applies to the xticklabels. See more here: https://seaborn.pydata.org/generated/seaborn.heatmap.html