plotting two graphs on same row / creating create - python

df = pd_df_total_primary_Y.set_index('EthnicGroups_EthnicGroup1Desc')
df1 = pd_df_total_general_Y.set_index('EthnicGroups_EthnicGroup1Desc')
df[["P_Y_Count20", "P_Y_Count16", "P_Y_Count12", "P_Y_Count08","P_Y_Count04", "P_Y_Count00"]].plot.bar()
plt.title('total_primary_Y');
df1[["G_Y_Count20", "G_Y_Count16", "G_Y_Count12", "G_Y_Count08", "G_Y_Count04", "G_Y_Count00"]].plot.bar()
plt.title('total_general_Y');
I am trying to plot these graph on the same row and then add two more graphs below them. I am struggling to get it to work. how can i do it?

Related

How to show a Holoviews Heatmap

I have a function that creates a Holoviewa heatmap. If I save the heatmap using hv.save(heatmap, 'heatmap.html') it works great! I just cannot figure out how to show the plot without having to save it. The same script generates two density plots with Plotly and using .show() and pops the plot up in my browser.
I am NOT using jupyter notebook and have been starting the bokeh server from a DOS prompt. I am working inside PyCharm Community with Python 3.10. Though if I could do it all from inside the script that would be easier.
def gen_heat_map(df: pandas.DataFrame, freq: float) -> holoviews.HeatMap:
"""
Uses a single frequency upon which to build the heatmap.
:param df: pandas.Dataframe containing data read from a JSON file
:param freq: The frequency to build the heatmap out of
:return: Holoviews Heat Map
"""
# Select a single frequency upon which to build the heatmap
single_frq = df[df.centerFrequency == freq].reset_index(drop=True)
# create a second dataframe from each transmission
sec_df = pd.DataFrame()
for index, row in single_frq.iterrows():
sec_df = sec_df.append(make_by_second(row), ignore_index=True)
min_df = sec_df.set_index('time').resample('1min').mean().reset_index().replace(np.nan, -160)
with pd.option_context('display.max_columns', None):
print(min_df)
min_df["Minute"] = min_df["time"].dt.strftime("%M")
min_df["Hour"] = min_df['time'].dt.strftime("%H")
heatmap = hv.HeatMap(min_df, ['Minute', 'Hour'], ['power', 'time'])
heatmap.opts(radial=True,
width=750,
height=750,
tools=['hover'],
colorbar=True,
cmap='bokeh',
start_angle=-np.pi * 7 / 24,
title='Frequency Power Level Radial Heat Map'
)
return heatmap
heatmap = gen_heat_map(df, 929612500.0)
The function gen_heat_map takes a large Pandas Dataframe of data read from a JSON file plus a single frequency and generates the heat map. It is trying to display this resultant heat map that is the issue. I can do so through Holoviz's Panel toolkit but I would like to find a simpler solution.
Suggestions?
Thanks,
Doug

Producing a plot using a for loop

I want to select from a dataframe based on a name. Then I want to plot the data on a single graph using a for loop.
df = pd.read_csv ('Kd.csv')
watertype = ['Evian','Volvic','Buxton']
for type in watertype:
sdf = df[(df['Water']==type)]
Na = sdf.iloc[:,13]
Kd = sdf.iloc[:,2]
plt.plot(Na,Kd,'o')
plt.show()`
Multiple graphs produced instead of overlaying them on a single graph.

How to order the columns in a pandas data frame non-alphabetically and to merge the cells of a matplotlib table?

I am trying to plot a table in python. I have it working and such...but when I plot my table, it doesn't plot the Pass/Fail column at the end how I have it written. It seems that the columns are being displayed in alphabetical order.
How do I disable this.
I want to add the last column but just as one row. Basically a big check box but when I do that it gives me an error that the arrays must be the same length, which makes sense...but how can I go around this and just have one large column with no rows..?
import pandas as pd
import matplotlib.pyplot as plt
MinP_M=5
Min_M=6
Per_M=7
Per_G=8
Per2_M=9
PerFlat_M=10
MaxPL_M=11
Max_M=12
GF_M =13
fig1 = plt.figure()
fig1.set_size_inches(8.7,11.75,forward=True)
ax1=fig1.add_subplot(111)
ax1.axis('off')
ax1.axis('tight')
data2={'Min':['%s'%MinP_M,'%s'%Min_M,'',''],
'Typ':['%s'%Per_M,'%s'%Per_G,'%s'%Per2_M,'+/- %s'%PerFlat_M],
'Max':['%s'%MaxPL_M,'','%s'%Max_M,'+/- %s'%GF_M],
'Pass/Fail':['','','','']
}
df2 = pd.DataFrame(data2)
the_table2=ax1.table(cellText=df2.values,colWidths=[0.15]*5,rowLabels=['A','B','C', 'D'],colLabels=df2.columns,loc='center')
plt.show()
The first part is relatively easy to solve. As you create your pandas data frame using a dict, the order of keywords and thus the order of columns is not fixed. To get the ordering correct, use the columns keyword. The second part was a bit more tricky. The solution I found here is to overlay your original table with a second table and then adding another cell to that second table that has the same height as the four cells of the original table. For that you have to first obtain the cell dictionary from the table instance and sum up the heights of the table rows. Please see the code below:
import pandas as pd
import matplotlib.pyplot as plt
MinP_M=5
Min_M=6
Per_M=7
Per_G=8
Per2_M=9
PerFlat_M=10
MaxPL_M=11
Max_M=12
GF_M =13
fig1 = plt.figure()
##this line entirely messed up the plot for me (on Mac):
##fig1.set_size_inches(8.7,11.75,forward=True)
ax1=fig1.add_subplot(111)
ax1.axis('off')
ax1.axis('tight')
data2={'Min':['%s'%MinP_M,'%s'%Min_M,'',''],
'Typ':['%s'%Per_M,'%s'%Per_G,'%s'%Per2_M,'+/- %s'%PerFlat_M],
'Max':['%s'%MaxPL_M,'','%s'%Max_M,'+/- %s'%GF_M],
'Pass/Fail':['','','','']
}
##fix the column ordering with a list:
keys = ['Min', 'Typ', 'Max', 'Pass/Fail']
df2 = pd.DataFrame(data2, columns=keys)
##defining the size of the table cells
row_label_width = 0.05
col_width = 0.15
col_height = 0.05
the_table2=ax1.table(
cellText=df2.values,
colWidths=[col_width]*4,
rowLabels=['A','B','C', 'D'],
colLabels=df2.columns,
##loc='center', ##this has no effect if the bbox keyword is used
bbox = [0,0,col_width*4,col_height*5],
)
celld = the_table2.get_celld()
##getting the heights of the header and the columns:
row_height_tot = 0
for (i,j),cell in celld.items():
if j==3 and i>0: #last column, but not the header
row_height_tot += cell.get_height()
the_table3=ax1.table(
cellText=['0'], ##cannot be empty
colLabels=df2.columns[-1:],
colWidths=[col_width],
bbox = [col_width*3,0,col_width,col_height*5],
)
the_table3.add_cell(1,0,col_width,row_height_tot)
fig1.tight_layout()
plt.show()
I had to turn off some of your formatting options as they gave weird results on my computer. If you want to have the table centred, play with the bbox options in the table commands. The final result looks like this:
Hope this helps.

Python sort_values plot is inverted

new Python learner here. This seems like a very simple task but I can't do it to save my life.
All I want to do is to grab 1 column from my DataFrame, sort it, and then plot it. THAT'S IT. But when I plot it, the graph is inverted. Upon examination, I find that the values are sorted, but the index is not...
Here is my simple 3 liner code:
testData = pd.DataFrame([5,2,4,2,5,7,9,7,8,5,4,6],[9,4,3,1,5,6,7,5,4,3,7,8])
x = testData[0].sort_values()
plt.plot(x)
edit:
Using matplotlib
If you're talking about ordering values sequentially on the x-axis like 0, 1, 2, 3, 4 ... You need to re-index your values.
x = testData[0].sort_values()
x.index = range(len(x))
plt.plot(x)
Other than that if you want your values sorted in the data frame but displayed by order of index then you want a scatter plot not a line plot
plt.scatter(x.index, x.values)

remove overlay text from pandas boxplot

I am trying to remove the overlay text on my boxplot I created using pandas. The code to generate it is as follows (minus a few other modifications):
ax = df.boxplot(column='min2',by=df['geomfull'],ax=axes,grid=False,vert=False, sym='',return_type='dict')
I just want to remove the "boxplot grouped by 0..." etc. and I can't work out what object it is in the plot. I thought it was an overflowing title but I can't find where the text is coming from! Thanks in advance.
EDIT: I found a work around which is to construct a new pandas frame with just the relevant list of things I want to box (removing all other variables).
data = {}
maps = ['BA4','BA5','BB4','CA4','CA5','EA4','EA5','EB4','EC4','EX4','EX5']
for mapi in maps:
mask = (df['geomfull'] == mapi)
arr = np.array(df['min2'][mask])
data[mapi] = arr
dfsub = pd.DataFrame(data)
Then I can use the df.plot routines as per examples....
bp = dfsub.plot(kind='box',ax=ax, vert=False,return_type='dict',sym='',grid=False)
This produces the same plot without the overlay.

Categories

Resources