I would love to have your input how to solve this issues.
I have a time series (x=date) ggplot with bars (y=cmass1), and my bars are on position="stack" using a factor (polymer) to display polymer types for each sample.
Each bar represents another sampling station (factor sample), and I would like to identify which bar is which station (factor "sample" in my data frame) and add the label above each bar.
I've come so far that I can show the "sample" labels, but then there appear multiple replicated labels (one for each data point in the stacked parts of the bar). But I need only one per bar!
Can someone help, how I can create one label per bar (above each bar) that identifies which sample it is (without manual annotation)?
Additional date display problem:
Also, on one date (11 November) I have two samples, and in this plot they are cumulated, but I would need them next to each - so two as stacked bars on one date (like with besides=TRUE, but that doesn't seem to work with stacked bars). Any help here?
I hope it is clear, and thanks so much for any help!
Cleo
The plot, where S1, S2 etc should be above each bar as a label
Dataframe view_partial
ggplot(Merg1[!(is.na(Merg1$campaign)),] ,
aes(date,y=cmass1,group_by(sample1), fill=polymer))+
geom_col(show.legend = T, alpha = 0.8, position="stack",
stat="identity")+
xlab("July 2021 November 2021 February 2022 July 2022") +
facet_grid(.~campaign, scales = "free_x",space = "free_x" )+
geom_point(aes(y=montsouris_mm*20 ),color="#A3009F",size=2.5, position="dodge")+
geom_point(aes(y=melun_mm*20 ),color="#558C8C",size=2.5, position="dodge")+
geom_point(aes(y=fauville_mm*20 ),color="#E8DB7D",size=2.5, position="dodge")+
scale_y_continuous(expand = c(0,0),limits=c(0, 1000), breaks = seq(0, 1000, by = 100),
sec.axis = sec_axis(~./20, name = "Precipitation (mm)",
breaks = seq(0, 50, by = 10))) +
scale_x_date(date_breaks = "1 day", labels = date_format("%d"))+
ylab(expression(bold(paste(~C[mass]~ (µg ~L^{-1}))))) +
theme(axis.text.x=element_text(color="black", size=12, angle=0, vjust=0.5),
axis.text.y=element_text(color="black", size=12, angle=0, vjust=0.5),
axis.ticks.x=element_blank(),
axis.title.x = element_text(size = 12,
color = "black",
face = "bold"),
strip.background = element_rect(color="white", fill="white", size=0.5,
linetype="solid"),
legend.text = element_text(color = "black",size = 12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(size = 0.5,colour="#D4CDCB",
fill="white" ,linetype = 'solid'),
legend.key.height= unit(0.2, 'cm'),
legend.key.width= unit(0.4, 'cm'))+
scale_fill_manual(name = "", values = MPcol, breaks = c("abs","acrylic", "acrylic paints", "alkyd","epoxy", "pa", "pan_acrylic fibre", "pe", "polyester", "pp", "ps", "pu", "pvac", "pvc", "sbr", "vinyl copolymer"),
labels=c("Acrylonitrile butadiene styrene (ABS)","Acrylic", "Acrylic paint", "Alkyd", "Epoxy", "Polyamide (PA)", "Pan acrylic fibre", "Polyethylene (PE)", "Polyester (PET)", "Polypropylene (PP)", "Polystyrene (PS)", "Polyurethane (PU)", "Polyvinyl acetate (PVAC)", "Poly vinyl chloride (PVC)", "Styrene butadiene rubber (SBR)", "Vinyl copolymere"))+
theme(legend.position = "bottom",
legend.background = element_rect(fill = "#FFFCFB", # Background
colour = 1),
legend.title = element_text(family = "sans",
color = "black",
size = 10,
face = 2)) +
theme(panel.grid.minor = element_line(color = "#D4CDCB",
size = 0.15,
linetype = 2))+
theme(strip.text.x = element_text(size=14, face="bold",
vjust = 2, color="black")) +
#geom_text( label = label1, vjust = -1, position = position_dodge(0.90), size = 3, hjust=-0.5)+
labs(title = "Microplastics",
subtitle = "Mass Concentrations by Polymer Types",
caption = "Still missing S4 repeated samples and M1 in Campaign D. S4 in Campaign C cumulated repetaed samples. MPs >300µm are excluded, also natural particles are excluded.",
tag = "Fig. 5-1") +
theme(plot.title = element_text(family = "serif", # Font family
face = "bold", # Font face
color = 1, # Font color
size = 16, # Font size
hjust = 1, # Horizontal adjustment
vjust = 1, # Vertical adjustment
angle = 0, # Font angle
lineheight = 1, # Line spacing
margin = margin(5, 0, 0, 0)), # Margins (t, r, b, l)
plot.subtitle = element_text(family = "serif",
hjust = 1, size=14), # Subtitle customization
plot.caption = element_text(hjust = 0.25, size =11,
family = "serif",face = "italic"), # Caption customization
plot.tag = element_text(face = "italic"), # Tag customization
plot.title.position = "plot", # Title and subtitle position ("plot" or "panel")
plot.caption.position = "panel", # Caption position ("plot" or "panel")
plot.tag.position = "top") # Tag position
Please help me to draw a simple line chart in python turtle module based on user input. I am able to draw the x and y axis. The x-axis tick is equal to the number of data points. For example, if the user input is 5 then it will be 5 ticks labelled 1 to 5 and so on. However, the tick for y-axis has 6 ticks(fixed) labelled from 0 to 20, increment in 4(0,4,8..20).
I can't figure out the logic for plotting y values based on user input. For example, if the user types 15, it needs to go to the corresponding data point in y axis and put a dot in there. Similarly, if the user types 5 it needs to go the corresponding value in y axis as well plus connecting to the preceding data point(in this case connecting 15 from previous point to 5 in the current point) and so on based on user input. Also, my x and y labeling could be done it in a better way. Here is my code. Any help is much appreciated. Please look at the image description, I am aiming for similar results.
import turtle as t
import time
wn = t.Screen() # create a window for the turtle to draw on
wn.title("Turtle Demo") # the title to show at the top of the window
WINDOW_WIDTH = 500 # size constants for easy changing
WINDOW_HEIGHT = 500
wn.setup(WINDOW_WIDTH, WINDOW_HEIGHT, 200, 10) # specify window size (width, height)
user_input = t.numinput(title='Line graph', prompt='How many data points:')
x_max = user_input
# print(userInput)
# graph window
x_min = 0
y_min = 0
y_max = 5
# tick info
t_l = 0.1
x_t_space = 1
y_t_space = 1
wn.setworldcoordinates(x_min, y_min, x_max, y_max)
# Draw x-axis
t.tracer(10)
t.hideturtle()
t.up()
t.goto(x_min, 0)
t.down()
t.goto(user_input, 0.0)
t.up()
# Draw the y-axis
t.goto(0.0, y_min)
t.down()
t.goto(0.0, y_max)
t.up()
# Draw the x tick marks
n_x_ticks = int((x_max - x_min) / x_t_space) + 1
for tick in range(n_x_ticks):
loc = x_min + tick * x_t_space
t.up()
t.goto(loc, -t_l * 0.4)
t.down()
t.goto(loc, t_l * 0.4)
t.up()
t.write(tick)
# Draw the y tick marks
y_label = range(0, 21, 4)
n_y_ticks = int((y_max - y_min) / y_t_space) + 1
for tick in range(n_y_ticks):
loc = y_min + tick * y_t_space
t.up()
t.goto(-t_l * 0.4, loc)
t.down()
t.goto(t_l * 0.4, loc)
for i in y_label:
tick = i
print(tick)
t.write(tick, align="left", font=("Arial", 8, "normal"))
# get user input and plot the y value as long as user needed.
# the below code marks the value in the x-axis itself, that is not what I want.
#I want the value to get plotted outside the axis as in the normal line chart
t.backward
t.goto(0, 0)
t.tiltangle(45)
user_value = t.numinput(title='Line graph', prompt='Enter the first value:')
t.shape("square")
t.stamp()
t.forward(user_value)
user_value = t.numinput(title='Line graph', prompt='Enter the next value:')
t.shape("square")
t.stamp()
t.forward(user_value)
user_value = t.numinput(title='Line graph', prompt='Enter the next value:')
t.shape("square")
t.stamp()
t.forward(user_value)
Line chart
The input size is defined by the user and you want the same size of y-values.
Do the following:
for i in range (0, int(user_input)):
user_value = t.numinput(title='Line graph', prompt='Enter the value:')
t.shape("square")
t.stamp()
t.forward(user_value)
EDIT:
Just use matplotlib, it is very simple
import numpy as np
import matplotlib.pyplot as plt
user = int(input("Enter number of data: "))
x = np.arange(user)
y = []
for i in range (0, user):
value = int(input("Enter value: "))
y.append(value)
y = np.array(y)
plt.scatter(x, y)
plt.plot(x, y)
plt.show()
Input:
Enter number of data: 9
Enter value: 10
Enter value: 30
Enter value: 20
Enter value: 50
Enter value: 60
Enter value: 35
Enter value: 38
Enter value: 12
Enter value: 31
Output:
which is the same as the chart you provided.
I got placed in a coding class and I have no idea what I'm doing.
This is supposed to create a nametag for a project that I have to finish by tonight.
Please help me figure out how to make the horizontal zigzags vertical.
t.pensize(24)
def zig_zag_line(num_zig_zags):
zig_zag = 0
t.setheading(20)
while zig_zag < num_zig_zags:
t.forward(40)
t.right(40)
t.forward(40)
t.left(40)
zig_zag = zig_zag + 1
def write_name(id_name):
style = ('Times New Roman', 70, 'bold')
t.write(id_name, font=style, align='center')
def white_space():
t.color("white")
t.pensize(26)
for i in range(3):
t.pu()
t.goto(-260, i * 30)
t.pd()
zig_zag_line(7)
i = i + 30
color1 = input("What is color 1?: ")
color2 = input("What is color 2?: ")
color3 = input("What is color 3?: ")
for i in range(-30, 33, 3):
if i % 9 == 0:
t.color(color1)
elif i % 9 == 3:
t.color(color2)
else:
t.color(color3)
t.pu()
t.goto(-260, i * 10)
t.pd()
zig_zag_line(7)
white_space()
t.color("black")
t.pu()
t.goto(0, 5)
t.pd()
write_name("")
t.hideturtle()
I just want to change the direction in which the zigzags are going. If anyone knows how to help I would really appreciate because I know virtually nothing about how to code this. I've been trying to figure it out but I'm going solely off the notes I've taken in class and I can't seem to figure out just how to make it go from horizontal to vertical.
There are two things we need to do: first swap our sense of x and y:
t.goto(-260, i * 10)
becomes:
t.goto(i * 10, -260)
and change our initial heading:
t.setheading(20)
becomes:
t.setheading(20 + 90)
A rework of the zigzag lines part of your code to make them vertical:
import turtle as t
def zig_zag_line(num_zig_zags):
t.setheading(110)
for _ in range(num_zig_zags):
t.forward(40)
t.right(40)
t.forward(40)
t.left(40)
color1 = input("What is color 1? ")
color2 = input("What is color 2? ")
color3 = input("What is color 3? ")
width, height = t.window_width(), t.window_height()
t.setup(height, width) # swap window wide and high
t.pensize(24)
t.speed('fastest') # because I have no patience
for i in range(-30, 33, 3):
if i % 9 == 0:
t.color(color1)
elif i % 9 == 3:
t.color(color2)
else:
t.color(color3)
t.penup()
t.goto(i * 10, -260)
t.pendown()
zig_zag_line(7)
t.hideturtle()
t.exitonclick()
I've included an optional third fix which is to swap the width and height of the window itself. Since the window might be sized to fit the horizontal drawing, we need to change it to fit the vertical drawing.
I want to define a Animation which change color of a mobject between two colors during specific time period.
E.g. a mobject with color RED, I want to set its color between RED and YELLOW back and forth 5 times during 2 seconds.
Below is my code (can change color, but the interval is not even), just want to confirm whether it is the correct way to do this, thanks!:
class ChangeColor(Animation):
def interpolate_submobject(self, submobject, starting_sumobject, alpha):
m = int(alpha * 10) % 2
if m == 0:
submobject.set_color(RED)
else:
submobject.set_color(YELLOW)
By default, alpha increment is smooth, so you have to change it to linear so that it is displayed correctly.
class ChangeColor(Animation):
CONFIG={
"rate_func":linear
}
def interpolate_submobject(self, submobject, starting_sumobject, alpha):
m = int(alpha * 10) % 2
if m == 0:
submobject.set_color(RED)
else:
submobject.set_color(YELLOW)
I have sequence of tuples containing filename and number.
I want to draw column graph where on X axis I have filenames.
My problem is that now only 3 labels (filenames) are shown under X axis. That's probably because more can't fit on screen. Or maybe X axis interval is wrong?
How to make chart display all filenames? Maybe there is a way to rotate those labels 90 degrees counterclockwise to make room for more labels?
You should be able to use:
|> Chart.WithXAxis (LabelStyle = ChartTypes.LabelStyle(Angle = -45, Interval = 1.0))
The angle of -45 gives a nice slope and the interval of 1.0 means nothing is excluded.
Here's a proof of concept I knocked up in FSI:
#load "C:/Somewhere/packages/FSharp.Charting.0.90.7/FSharp.Charting.fsx"
open FSharp.Charting;;
let data =
[
("Foo.jpg", 12)
("Bar.jpg", 22)
("Another.doc", 8)
("OneMore.txt", 15)
("LastOne.txt", 17)
("ReallyLastOne.txt", 6)
("Foo.jpg", 12)
("Bar.jpg", 22)
("Another.doc", 8)
("OneMore.txt", 15)
("LastOne.txt", 17)
("ReallyLastOne.txt", 6)
];;
data
|> Chart.Line
|> Chart.WithXAxis (LabelStyle = ChartTypes.LabelStyle(Angle = -45, Interval = 1.0))
;;