Spaces:
Sleeping
Sleeping
Create plotly_im.py
Browse files- plotly_im.py +71 -0
plotly_im.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import pandas as pd
|
2 |
+
# import plotly.express as px
|
3 |
+
|
4 |
+
# def plot_to_html(df: pd.DataFrame, x: str, y: str, labels: dict, color_based_on: str, y_label: str = "", x_label: str = "", title: str = "", filepath: str = "plot") -> str:
|
5 |
+
# # Create a bar plot with a greyscale color theme
|
6 |
+
# fig = px.bar(
|
7 |
+
# df,
|
8 |
+
# x=x,
|
9 |
+
# y=y,
|
10 |
+
# title=title,
|
11 |
+
# labels=labels,
|
12 |
+
# color=color_based_on,
|
13 |
+
# color_continuous_scale=px.colors.sequential.gray_r
|
14 |
+
# )
|
15 |
+
|
16 |
+
# # Update layout for appearance
|
17 |
+
# fig.update_layout(
|
18 |
+
# xaxis_title=x_label,
|
19 |
+
# yaxis_title=y_label, # Adding the y_label explicitly
|
20 |
+
# template="plotly_white", # Light background for greyscale contrast
|
21 |
+
# title_x=0.5, # Center the title
|
22 |
+
# )
|
23 |
+
|
24 |
+
# fig.write_image(f"{filepath}.png")
|
25 |
+
# return f"{filepath}.png"
|
26 |
+
|
27 |
+
import pandas as pd
|
28 |
+
import plotly.express as px
|
29 |
+
|
30 |
+
def plot_to_html(df: pd.DataFrame, x: str, y: str, labels: dict, color_based_on: str, y_label: str = "", x_label: str = "", title: str = "", filepath: str = "plot") -> str:
|
31 |
+
# Create a bar plot with a greyscale color theme
|
32 |
+
fig = px.bar(
|
33 |
+
df,
|
34 |
+
x=x,
|
35 |
+
y=y,
|
36 |
+
title=title,
|
37 |
+
labels=labels,
|
38 |
+
color=color_based_on,
|
39 |
+
color_continuous_scale=px.colors.sequential.Jet
|
40 |
+
)
|
41 |
+
|
42 |
+
# Update layout for appearance
|
43 |
+
fig.update_layout(
|
44 |
+
xaxis_title=x_label,
|
45 |
+
yaxis_title=y_label, # Adding the y_label explicitly
|
46 |
+
template="plotly_white", # Light background for greyscale contrast
|
47 |
+
title_x=0.5, # Center the title
|
48 |
+
)
|
49 |
+
|
50 |
+
fig.write_image(f"{filepath}.png")
|
51 |
+
return f"{filepath}.png"
|
52 |
+
|
53 |
+
def plot_pie_chart(df: pd.DataFrame, names: str, values: str, labels: dict, title: str = "", filepath: str = "plot") -> str:
|
54 |
+
# Create a pie chart
|
55 |
+
fig = px.pie(
|
56 |
+
df,
|
57 |
+
names=names,
|
58 |
+
values=values,
|
59 |
+
title=title,
|
60 |
+
labels=labels,
|
61 |
+
color_discrete_sequence=px.colors.sequential.Jet
|
62 |
+
)
|
63 |
+
|
64 |
+
# Update layout for appearance
|
65 |
+
fig.update_layout(
|
66 |
+
template="plotly_white", # Light background for greyscale contrast
|
67 |
+
title_x=0.5 # Center the title
|
68 |
+
)
|
69 |
+
|
70 |
+
fig.write_image(f"{filepath}.png")
|
71 |
+
return f"{filepath}.png"
|