How to remove the overlapping in threshold_scale of Choropleth Map? - folium

#ChoroplethMap
bins = list(state_avg_value["price"].quantile([0, 0.25, 0.5, 0.75, 1]))
#state_avg_value_max= state_avg_value['price'].max()
m = folium.Map(location=[48, -102], zoom_start=3)
folium.Choropleth(
geo_data=state_geo,
data=state_avg_value,
columns=["state", "price"],
key_on="feature.properties.name",
fill_color="BuPu",
fill_opacity=0.7,
line_opacity=0.5,
legend_name="Price (in dollars)",
bins=bins,
reset=True,
).add_to(m)
m
#Q. How to remove the overlapping in threshold_scale?
#[ChoroplethMap][1]
#[1]: https://i.stack.imgur.com/Xi4A6.jpg

Related

vectorising images using matrix multiplication

I want to apply the homography matrix (3x3) to an image in order to convert it in the frame of reference of another image. so to create a panorama effect. Currently I have a working code that involves a nested for loop which iterates over all the pixel array and applies the transform the the vector of 3x1 pixels. I would like speed up the code and replace the nested for loop with a matrix multiplication so that multiply all the position indices by the homography matrix in one go.
This is my current code:
H = calcBestHomography(pts1b , pts3)
for i in range(im1.shape[1]):
for j in range(im1.shape[0]):
pt1 = np.array([i, j, 1])
pt3 = H # pt1.T
px = int(pt3[0] / pt3[2])
py = int(pt3[1] / pt3[2])
if (px > 0 and px < im3.shape[1] and py> 0 and py < im3.shape[0]):
im1[j , i , : ] = im3[py , px ,:]
thank you
this is my current attempt but my main issue is that since the homography matrix is a 3x3 I cannot pass a matrix that has more than 3 columns
ax0 = np.linspace(0, im1.shape[0]-1, im1.shape[0])
ax1 = np.linspace(0, im1.shape[1]-1, im1.shape[1])
ax2 = np.ones((2740 , 880))
ax2_3d = np.expand_dims(ax2, axis=2)
pts1 = np.meshgrid(ax0 , ax1 )
pts1 = np.stack(pts1 , axis=-1)
result = np.concatenate((pts1, ax2_3d), axis=2)
pt3 = (result) # H
px = (pt3[0] / pt3[2])
py = (pt3[1] / pt3[2])

How to expand bars over the month on the x-axis while being the same width?

for i in range(len(basin)):
prefix = "URL here"
state = "OR"
basin_name = basin[i]
df_orig = pd.read_csv(f"{prefix}/{basin_name}.csv", index_col=0)
#---create date x-index
curr_wy_date_rng = pd.date_range(
start=dt(curr_wy-1, 10, 1),
end=dt(curr_wy, 9, 30),
freq="D",
)
if not calendar.isleap(curr_wy):
print("dropping leap day")
df_orig.drop(["02-29"], inplace=True)
use_cols = ["Median ('91-'20)", f"{curr_wy}"]
df = pd.DataFrame(data=df_orig[use_cols].copy())
df.index = curr_wy_date_rng
#--create EOM percent of median values-------------------------------------
curr_wy_month_rng = pd.date_range(
start=dt(curr_wy-1, 10, 1),
end=dt(curr_wy, 6, 30),
freq="M",
)
df_monthly_prec = pd.DataFrame(data=df_monthly_basin[basin[i]].copy())
df_monthly_prec.index = curr_wy_month_rng
df_monthly = df.groupby(pd.Grouper(freq="M")).max()
df_monthly["date"] = df_monthly.index
df_monthly["wy_date"] = df_monthly["date"].apply(lambda x: cal_to_wy(x))
df_monthly.index = pd.to_datetime(df_monthly["wy_date"])
df_monthly.index = df_monthly["date"]
df_monthly["month"] = df_monthly["date"].apply(
lambda x: calendar.month_abbr[x.month]
)
df_monthly["wy"] = df_monthly["wy_date"].apply(lambda x: x.year)
df_monthly.sort_values(by="wy_date", axis=0, inplace=True)
df_monthly.drop(
columns=[i for i in df_monthly.columns if "date" in i], inplace=True
)
# df_monthly.index = df_monthly['month']
df_merge = pd.merge(df_monthly,df_monthly_prec,how='inner', left_index=True, right_index=True)
#---Subplots---------------------------------------------------------------
fig, ax = plt.subplots(figsize=(8,4))
ax.plot(df_merge.index, df_merge["Median ('91-'20)"], color="green", linewidth="1", linestyle="dashed", label = 'Median Snowpack')
ax.plot(df_merge.index, df_merge[f'{curr_wy}'], color='red', linewidth='2',label='WY Current')
#------Seting x-axis range to expand bar width for ax2
ax.bar(df_merge.index,df_merge[basin[i]], color = 'blue', label = 'Monthly %')
#n = n + 1
#--format chart
ax.set_title(chart_name[w], fontweight = 'bold')
w = w + 1
ax.set_ylabel("Basin Precipitation Index")
ax.set_yticklabels([])
ax.margins(x=0)
ax.legend()
#plt.xlim(0,9)
#---Setting date format
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
#---EXPORT
plt.show()
End result desired: Plotting both the monthly dataframe (df_monthly_prec) with the daily dataframe charting only monthly values (df_monthly). The bars for the monthly DataFrame should ideally span the whole month on the chart.
I have tried creating a secondary axis, but had trouble aligning the times for the primary and secondary axes. Ideally, I would like to replace plotting df_monthly with df (showing all daily data instead of just the end-of-month values within the daily dataset).
Any assistance or pointers would be much appreciated! Apologies if additional clarification is needed.

geom_bar for total counts of binned continuous variable

I'm really struggling to achieve what feels like an incredibly basic geom_bar plot. I would like the sum of y to be represented by one solid bar (with colour = black outline) in bins of 10 for x. I know that stat = "identity" is what is creating the unnecessary individual blocks in each bar but can't find an alternative to achieving what is so close to my end goal. I cheated and made the below desired plot in illustrator.
I don't really want to code x as a factor for the bins as I want to keep the format of the axis ticks and text rather than having text as "0 -10", "10 -20" etc. Is there a way to do this in ggplot without the need to use summerise or cut functions on the raw data? I am also aware of geom_col and sat_count options but again, can't achive my desired outcome.
DF as below, where y = counts at various values of a continuous variable x. Also a factor variable of type.
y = c(1 ,1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 1,1, 2, 1, 2, 3, 2, 2, 1)
x = c(26.7, 28.5, 30.0, 34.8, 35.0, 36.4, 38.6, 40.0, 42.1, 43.7, 44.1, 45.0, 45.5, 47.4, 48.0, 57.2, 57.8, 64.2, 65.0, 66.7, 68.0, 74.4, 94.1)
type = c(rep("Type 1", 20), "Type 2", rep("Type 1", 2))
df<-data.frame(x,y,type)
Bar plot of total y count for each bin of x - trying to fill by total of type, but getting individual proportions as shown by line colour = black. Would like total for each type in each bar.
ggplot(df,aes(y=y, x=x))+
geom_bar(stat = "identity",color = "black", aes(fill = type))+
scale_x_binned(limits = c(20,100))+
scale_y_continuous(expand = c(0, 0), breaks = seq(0,10,2)) +
xlab("")+
ylab("Total Count")
Or trying to just have the total count within each bin but don't want the internal lines in the bars, just the outer colour = black for each bar
ggplot(df,aes(y=y, x=x))+
geom_col(fill = "#00C3C6", color = "black")+
scale_x_binned(limits = c(20,100))+
scale_y_continuous(expand = c(0, 0), breaks = seq(0,10,2)) +
xlab("")+
ylab("Total Count")
Here is one way to do it, with previous data transformation and geom_col:
df <- df |>
mutate(bins = floor(x/10) * 10) |>
group_by(bins, type) |>
summarise(y = sum(y))
ggplot(data = df,
aes(y = y,
x = bins))+
geom_col(aes(fill = type),
color = "black")+
scale_x_continuous(breaks = seq(0,100,10)) +
scale_y_continuous(expand = c(0, 0),
breaks = seq(0,10,2)) +
xlab("")+
ylab("Total Count")

changing the axis values in matplotlib plot

In continuation of this accepted answer plotting a beautiful timeseries plot I want to change the y axis values after the plot by dividing a floating point number (0.2) with the y axis values so that the values of the y axis will be 0.0,0.5,1.0,1.5,2,2.5,3.0. without changing the timeseries.
The code is
data = pd.DataFrame(np.loadtxt("data_3_timeseries"), columns=list('ABC'))
data['B'] = data['B'].apply(lambda x: x + 0.3)
data['C'] = data['C'].apply(lambda x: x + 0.6)
ax = data.plot()
for col, place, line in zip(list('ABC'), [5, 8, 10], ax.lines):
ax.plot(place, data[col][place], marker="*", c=line.get_color())
plt.show()
data_3_timeseries
-0.00831 -0.0213 -0.0182
0.0105 -0.00767 -0.012
0.00326 0.0148 -0.00471
-0.0263 -0.00318 0.011
0.012 0.0398 0.0117
-0.0156 -0.0133 0.02
-0.0482 -0.00783 -0.0162
0.0103 -0.00639 0.0103
0.0158 0.000788 -0.00484
-0.000704 -0.0236 0.00579
0.00151 -0.0135 -0.0195
-0.0163 -0.00185 0.00722
0.0207 0.00998 -0.0387
-0.0246 -0.0274 -0.0108
0.0123 -0.0155 0.0137
-0.00963 0.0023 0.0305
-0.0147 0.0255 -0.00806
0.000488 -0.0187 5.29e-05
-0.0167 0.0105 -0.0204
0.00653 0.0176 -0.00643
0.0154 -0.0136 0.00415
-0.0147 -0.00339 0.0175
-0.0238 -0.00284 0.0204
-0.00629 0.0205 -0.017
0.00449 -0.0135 -0.0127
0.00843 -0.0167 0.00903
-0.00331 7.2e-05 -0.00281
-0.0043 0.0047 0.00681
-0.0356 0.0214 0.0158
-0.0104 -0.0165 0.0092
0.00599 -0.0128 -0.0202
0.015 -0.0272 0.0117
0.012 0.0258 -0.0154
-0.00509 -0.0194 0.00219
-0.00154 -0.00778 -0.00483
-0.00152 -0.0451 0.0187
0.0271 0.0186 -0.0133
-0.0146 -0.0416 0.0154
-0.024 0.00295 0.006
-0.00889 -0.00501 -0.028
-0.00555 0.0124 -0.00406
-0.0185 -0.0114 0.0224
0.0143 0.0204 -0.0193
-0.0168 -0.00608 0.00178
-0.0159 0.0189 0.0109
-0.0213 -0.007 -0.0323
0.0031 0.0207 -0.00333
-0.0202 -0.0157 -0.0105
0.0159 0.00216 -0.0262
0.0105 -0.00292 0.00447
0.0126 0.0163 -0.0141
0.01 0.00679 0.025
0.0237 -0.0142 -0.0149
0.00394 -0.0379 0.00905
-0.00803 0.0186 -0.0176
-0.013 0.0162 0.0208
-0.00197 0.0313 -0.00804
0.0218 -0.0249 0.000412
-0.0164 0.00681 -0.0109
-0.0162 -0.00795 -0.00279
-0.01 -0.00977 -0.0194
-0.00723 -0.0464 0.00453
-0.000533 0.02 -0.0193
0.00706 0.0391 0.0194
I've tried to be as detailed with the comments as I can, I hope this will be clear:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
COLUMNS = ['A', 'B', 'C'] # If you have more columns you can add here
MARKS = [('A', 5), ('B', 8), ('C', 10), ('A', 20), ('C', 25)] # You can add here more marks
# Here You can add/edit colors for the lines and the markers, and add new columns if exists
COLORS_DICT = {'A': {'Line': 'Purple', 'Marker': 'Blue'},
'B': {'Line': 'Red', 'Marker': 'Green'},
'C': {'Line': 'Brown', 'Marker': 'Orange'}, }
FACTOR = 6 # the factor
SPACER = 1 # This spacer together with the factor will have the y axes with 0.5 gaps
MARKER = '*' # star Marker, can be altered.
LINE_WIDTH = 0.5 # the width of the lines
COLORS = True # True for colors False for black
data = pd.DataFrame(np.loadtxt("data_3_timeseries"), columns=COLUMNS)
for i, col in enumerate(COLUMNS): # iterating through the columns
data[col] = data[col].apply(lambda x: x * FACTOR + i * SPACER) # applying each column the factor and the spacer
ax = data.plot()
ax.get_legend().remove() # removing the columns' legend (If Colors is False there's no need for legend)
for col, line in zip(COLUMNS, ax.lines): # iterating through the column and lines
if COLORS:
line.set_color(COLORS_DICT[col]['Line'])
else:
line.set_color('Black')
line.set_linewidth(LINE_WIDTH)
for col, mark in MARKS:
ax.plot(mark, data[col][mark], marker=MARKER, c=COLORS_DICT[col]['Marker'] if COLORS else 'Black')
plt.show()

Ternary Countour Plot - Plotly

I have created the above ternary plot using the code below:
import plotly.figure_factory as ff
fig = ff.create_ternary_contour(np.stack((prob0,prob1,prob2)),some_calc_val,
pole_labels=[r'$\text{AxesA}$', r'$\text{AxesB}$', r'$\text{AxesC}$'],
interp_mode='cartesian',
ncontours=50,
colorscale='Jet',
showscale=True,
title=r'$\text{Plot}$')
fig.update_ternaries(
aaxis = dict(
tickmode = 'array',
ticklen = 10,
tickvals = [0.2, 0.4, 0.6, 0.8],
ticktext = [r'$0.2$', r'$0.4$', r'$0.6$', r'$0.8$']
),
baxis = dict(
tickmode = 'array',
ticklen = 10,
tickvals = [0.2, 0.4, 0.6, 0.8],
ticktext = [r'$0.2$', r'$0.4$', r'$0.6$', r'$0.8$']
),
caxis = dict(
tickmode = 'array',
ticklen = 10,
tickvals = [0.2, 0.4, 0.6, 0.8],
ticktext = [r'$0.2$', r'$0.4$', r'$0.6$', r'$0.8$']
)
)
fig.update_layout(width=600, height=600)
fig.show()
Data for refrence
prob0=[9.99960940e-01 6.03061907e-04 9.10372544e-12 9.99952169e-01
2.81419593e-04 2.17084140e-18 9.99882767e-01 5.63535132e-11
1.86320179e-25]
prob1=[3.90598546e-05 9.99396859e-01 6.40065936e-01 4.78313969e-05
5.71105924e-01 1.86904565e-07 5.85691843e-05 1.40045638e-07
1.96443635e-14]
prob2=[4.32181700e-19 7.88323607e-08 3.59934064e-01 5.03536073e-12
4.28612656e-01 9.99999813e-01 5.86636024e-05 9.99999860e-01
1.00000000e+00]
some_calc_val=[3.90598546e-05 6.03140740e-04 3.59934064e-01 4.78314019e-05
4.28894076e-01 1.86904565e-07 1.17232787e-04 1.40101991e-07
1.95399252e-14]
Questions:
I would like to Latexify the ticks for the color bar on the right, I have looked around in the layout but did not find any such color bar which I could update the ticks for
Is it possible to add a little space between the ternary plot and the color bar, the name of the AxesC falls right under the color bar which is not ideal for my use case.
Also is there a way to add labels similar to what is used in go.Countour() which has a showlabels key
Let me know if separate questions need to be posted for each of the above.
References before posting:
https://community.plotly.com/t/how-to-get-the-axis-ticks-of-a-scatter-plot-to-be-shown-in-latex-using-python-python-latex-representation/61302
https://community.plotly.com/t/format-ticks-add-km-at-the-end/60932