How to increase the size of bins of a cluster histo, with making sure that the cluster columns do not overlap? - size

This is the code:
############
reset session
$Data <<EOD
subject 2018/19 2021/22
M 31.46 28.96 -2.5
It 34.83 36.61 +1.8
In 33.71 33.33 -0.4
G 63.79 77.46 +13.7
P 92.86 56.52 -36.3
PS 67.86 60.87 -7.0
A 21.05 65.00 +43.9
D 53.33 54.35 +1.0
EOD
set term svg
set output "prova.svg"
set title "{/Arial:Bold=14 MEAN}\n {/Arial:Bold=14 2018/19 vs 2021/2022}"
set yrange [0:100]
set ytics (0, '10' 10, '20' 20, '30' 30,'40' 40, '50' 50,'60' 60, '70' 70,'80' 80, '90' 90,'100' 100)
set grid y
set ylabel "% TOT" font ',14'
set tics scale 0
set tics font ',14'
set style fill pattern 1 border lt -1
set boxwidth 1 absolute
set key noautotitle
set key font ',14'
set xzeroaxis ls 1 lc "black"
PosX(row,col) = 0
#myColors = "red orange yellow green cyan blue violet deeppink"
myColors = "0xff0000 0xffa500 0xffff00 0x80ff00 0x00ffff 0x0000ff 0x7f00ff 0xff1493"
myColor(i) = int(word(myColors,int(i+1)))
stats $Data u 0 nooutput # get number of columns
N = STATS_columns
plot for [col=2:N] $Data u ($0*N+col):col:(myColor($0)) skip 1 w boxes lc rgb var, \
for [col=2:N] '' u ($0*N+N/2.+1):(NaN):xtic(1) w boxes fill pattern col -1 lc "grey" ti columnheader(col),\
col=2 '' u ($0*N+col-3):col:(sprintf("%2.1f%%",$2)) with labels center offset 0, character 0.5 font ',9' notitle,\
col=3 '' u ($0*N+col-3):col:(sprintf("%+-.1f%%",$4)) with labels center offset 0, character 0.5 font ',9' tc lt 7 notitle
#END code
############
I want to try to increase the width of the bins (of the same size without overlap) in each bin cluster and increase the white gap beetwen the clusters to increase the size of the labels on top of the bins

Here is the slightly modified script:
Comments:
you can minimize the gaps between the groups, but if you have to show 16 bars, the space for the labels will be limited. So, in order to make the labels larger I would skip the percent sign in the label. It is already on the y-axis.
you don*t have to set the ytics manually, you can simply set ytics 10 (check help xtics)
column 4 in your data did not have a header. I added a header, but then I have to correct the automatic determination of number of columns via stats, i.e. N=STATS_columns-1 since the last column will not be used for plotting bars.
for the labels you have to skip 1 line because you are not using the header line.
define a function PosX() for the x-position of the bar and the labels. Use the variable Gap to tune the gap (Gap=0 no gap).
use the ternary operator (check help ternary) to define a color which depends on the value. And apply the color via tc rgb var (check help lc variable).
The script certainly can be further optimized.
Script:
### grouped bar chart with adjustable gaps
reset session
$Data <<EOD
subject 2018/19 2021/22 change
M 31.46 28.96 -2.5
It 34.83 36.61 +1.8
In 33.71 33.33 -0.4
G 63.79 77.46 +13.7
P 92.86 56.52 -36.3
PS 67.86 60.87 -7.0
A 21.05 65.00 +43.9
D 53.33 54.35 +1.0
EOD
set title "{/Arial:Bold=14 MEAN}\n {/Arial:Bold=14 2018/19 vs 2021/2022}"
set xrange[1:]
set ylabel "% TOT" font ',14'
set yrange [0:100]
set ytics 10
set grid y
set tics scale 0
set tics font ',14'
set style fill pattern 1 border lt -1
set boxwidth 1 absolute
set key noautotitle font ',14'
#myColors = "red orange yellow green cyan blue violet deeppink"
myColors = "0xff0000 0xffa500 0xffff00 0x80ff00 0x00ffff 0x0000ff 0x7f00ff 0xff1493"
myColor(i) = int(word(myColors,int(i+1)))
myLabelColor(v) = v>0? 0x00aa00 : v<0 ? 0xff0000 : 0x777777 # green,red,grey
stats $Data u 0 nooutput # get number of columns
N = STATS_columns-1
Gap = 0.25
PosX(col) = (column(col))*(N-1+Gap)
set term svg
set output "SO74502026.svg"
plot for [col=2:N] $Data u (PosX(0)+col):col:(myColor($0)) skip 1 w boxes lc rgb var, \
for [col=2:N] '' u (PosX(0)+N/2.+1):(NaN):xtic(1) w boxes fill pattern col-1 lc "grey" ti columnheader(col),\
col=2 '' u (PosX(0)+col):col:(sprintf("%2.1f",$2)) skip 1 w labels center offset 0, 0.5 font ',10',\
col=3 '' u (PosX(0)+col):col:(sprintf("%+-.1f",$4)):(myLabelColor($4)) skip 1 w labels center offset 0, 0.5 font ',10' tc rgb var
set output
### end of script
Result:

Related

Map of New York State counties with binned colors and legend

I am trying to make a county-level map of the state of New York. I would like to color each county based on their level of unionization. I need the map and legend to have four discrete colors of red, rather than a red gradient. I need the legend to display these four different colors with non-overlapping labels/ranges (e.g. 0-25; 26-50; 51-75; 76-100).
Here is my data:
fips unionized
1 36001 33.33333
2 36005 86.11111
3 36007 0.00000
4 36017 0.00000
5 36021 0.00000
6 36027 66.66667
7 36029 40.00000
8 36035 50.00000
9 36039 0.00000
10 36047 82.85714
11 36051 0.00000
12 36053 100.00000
13 36055 30.76923
14 36057 0.00000
15 36059 84.37500
16 36061 81.81818
17 36063 60.00000
18 36065 50.00000
19 36067 71.42857
20 36069 0.00000
21 36071 55.55556
22 36073 0.00000
23 36079 100.00000
24 36081 92.15686
25 36083 50.00000
26 36085 100.00000
27 36087 87.50000
28 36101 0.00000
29 36103 63.88889
30 36105 0.00000
31 36107 0.00000
32 36111 50.00000
33 36113 50.00000
34 36115 100.00000
35 36117 0.00000
36 36119 73.33333
37 36121 0.00000
38 36123 0.00000
I have successfully made the map with a gradient of colors, but cannot figure out how to make discrete colors in the map and legend.
Here is my code:
library(usmap)
library(ggplot2)
plot_usmap(regions = "counties", include = c("NY"), data = Z, values = "unionized") +
labs(title = "Percent Unionized", subtitle = "") +
scale_fill_continuous(low = "white", high = "red", na.value="light grey", name = "Unionization") + theme(legend.position = "right")
Thanks!
This could be achieved via scale_fill_binned and guide_bins. Try this:
library(usmap)
library(ggplot2)
plot_usmap(regions = "counties", include = c("NY"), data = Z, values = "unionized") +
labs(title = "Percent Unionized", subtitle = "") +
scale_fill_binned(low = "white", high = "red", na.value="light grey", name = "Unionization", guide = guide_bins(axis = FALSE, show.limits = TRUE)) +
theme(legend.position = "right")
A second option would be to bin the variable manually and use scale_fill_manual to set the fill colors which makes it easy to set the labels and has the advantage that it adds the NAs automatically. For the color scale I make use of colorRampPalette (By default colorRampPalette interpolates in rgb color space. To get fill colors like the one using scale_fill_binned you can add the argument space = "Lab".).
library(usmap)
library(ggplot2)
Z$union_bin <- cut_interval(Z$unionized, n = 4, labels = c("0-25", "26-50", "51-75", "76-100"))
plot_usmap(regions = "counties", include = c("NY"), data = Z, values = "union_bin") +
labs(title = "Percent Unionized", subtitle = "") +
scale_fill_manual(values = colorRampPalette(c("white", "red"))(5)[2:5],
na.value="light grey", name = "Unionization") +
theme(legend.position = "right")

matplotlib scatter plot using a 3rd data series to designate color

I have 3 data series that share the same index values:
series a
A 0.6
B 0.4
C 0.7
D 0.5
series b
A 0.8
B 0.4
C 0.7
D 0.5
series c
A 10
B 23
C 50
D 100
series a and b are my x and y axis. I would like to use series c to designate the color of the dots (if value at c > 80 then colors = red elif value at c > 20 then colors = blue).
This is what my code looks like so far:
colors = 'black' #default color
plt.scatter(a, b, s=np.pi*1, c=colors, alpha=0.5)
#this is what I'm looking for
#if value at c > 80 then colors = red elif value at c > 20 then colors = blue
plt.show()
this is what the finished graph would look like:
Thanks!
You want np.select to define color:
colors = np.select((c>80, c>20), ('r','b'), default='g')
plt.scatter(a,b, c=colors)
Output:
Another way, but not nearly as concise as #quang-hoang's.
Create a function with your criteria and then apply it to the df
def colors(row):
if row.v > 80: return 'red'
elif row.v > 20: return 'blue'
else: return 'green'
df['colors'] = df.apply(colors, axis=1)
Give you this:
x y v colors
A 0.6 0.8 10.0 green
B 0.4 0.4 23.0 blue
C 0.7 0.7 50.0 blue
D 0.5 0.5 100.0 red
df.plot.scatter('x', 'y', c=df.colors)

How to set dimensions of LineSeries

I have a line series that covers data from 1 to 20 along the X axis and 0 to 1 along the Y axis. My problem is that the x axis always starts at 0 and goes to the furthest value and the Y axis starts at 0 but only goes to the furthest value as well. How can I set these dimensions?
So for the Y axis this is what you want
plotModel.Axes.Add(new LinearAxis()
{
Title = "Y axis Name",
Position = AxisPosition.Left,
MinorStep = 0.1,
FilterMinValue = -1,
FilterMaxValue = 1
});
As for the X axis it seems to be more annoying handLabeling seems to be the only way.
plotModel.Axes.Add(new CategoryAxis()
{
Title = "X axis name",
Position = AxisPosition.Bottom,
Labels = {"1","2","3","4","5","6" } // ... upto 20
});
Then every item you add index the value of the Category label.
For example if I want an column of 64 on 3 in X axis then the code would look like this:
var item =new ColumnItem(62, 2); // index 2 being value "3"

How do I set LinePlot line thickness and style? (DigitalMicrograph script)

The scripting help documentation of DigitalMicrograph offers an example for setting LinePlot styles with respect of colour and fill (see example script below).
However, the ImageDisplay menu for LinePlots also allows setting line styles (dotted, dashed,...) line thickness and transparency. Can somebody give an example on how to set these values, please?
// create image and image document
ImageDocument imageDoc = CreateImageDocument( "New ImageDocument" )
number width = 256
number height = 5
image img := RealImage("Line Plot Test", 4, width, height )
img = sin( irow + icol/100 )
// add LinePlotImageDisplay to ImageDocument
ImageDisplay imgdsp = imageDoc.ImageDocumentAddImageDisplay( img, 3 )
imgdsp.LinePlotImageDisplaySetContrastLimits( -1.1, 1.1 )
imgdsp.LinePlotImageDisplaySetDoAutoSurvey( 0, 0 )
// draw fill and line for slice 0
imgdsp.LinePlotImageDisplaySetSliceDrawingStyle(0, 3)
// set line color to red
imgdsp.LinePlotImageDisplaySetSliceComponentColor(0, 0, 1, 0, 0)
// set fill color to yellow
imgdsp.LinePlotImageDisplaySetSliceComponentColor(0, 1, 0.9, 0.9, 0)
// draw fill for slice 1 and 2
imgdsp.LinePlotImageDisplaySetSliceDrawingStyle(1, 2)
imgdsp.LinePlotImageDisplaySetSliceDrawingStyle(2, 2)
// draw line for slice 3 and 4
imgdsp.LinePlotImageDisplaySetSliceDrawingStyle(3, 1)
imgdsp.LinePlotImageDisplaySetSliceDrawingStyle(4, 1)
imageDoc.ImageDocumentShow()
The commands you are looking for are:
void LinePlotImageDisplaySetSliceLineThickness( LinePlotImageDisplay lpid, Number slice_id, Number lineThickness )
void LinePlotImageDisplaySetSliceLineStyle( LinePlotImageDisplay lpid, Number slice_id, Number lineStyle )
void LinePlotImageDisplaySetSliceTransparency( LinePlotImageDisplay lpid, Number sliceIndex, Boolean doTransparent, Number transparency )
They are demonstrated in the example below. Note that the visibility of line styles depend on the number of points in a LinePlot. If the LinePlot has more data points than displayed pixels, you may not notice the line style as it is defined 'in between' data points:
// create image and image document
ImageDocument imageDoc = CreateImageDocument( "New ImageDocument" )
number width = 64
number height = 10
image img := RealImage("Line Plot Test", 4, width, height )
img = sin( irow + icol / iwidth * 2 * Pi() ) + ( irow < ( height / 2 ) ? 1.5 : -1.5 )
// add LinePlotImageDisplay to ImageDocument
ImageDisplay imgdsp = imageDoc.ImageDocumentAddImageDisplay( img, 3 )
imgdsp.LinePlotImageDisplaySetContrastLimits( -2.6, 2.6 )
imgdsp.LinePlotImageDisplaySetDoAutoSurvey( 0, 0 )
// Line style demo
for ( number i = 0 ; i < height / 2 ; i++ )
{
number index = i + height / 2
// Set Line - drawing (no fill)
imgdsp.LinePlotImageDisplaySetSliceDrawingStyle( index , 1 )
// Set black line
imgdsp.LinePlotImageDisplaySetSliceComponentColor( index , 0 , 0, 0, 0 )
// Set LineThickness
imgdsp.LinePlotImageDisplaySetSliceLineThickness( index , height / 2 - i + 1 )
// Set LineStyle
imgdsp.LinePlotImageDisplaySetSliceLineStyle( index , i )
}
// Transparecny demo
for ( number i = 0 ; i < height / 2 ; i++ )
{
number index = i
// Set Fill & Line - drawing
imgdsp.LinePlotImageDisplaySetSliceDrawingStyle( index , 1 + 2 )
// Set black fill & red line
imgdsp.LinePlotImageDisplaySetSliceComponentColor( index , 1 , 0 , 0 , 0 )
imgdsp.LinePlotImageDisplaySetSliceComponentColor( index , 0 , 255 , 0 , 0 )
// Set transparency ( 70% transparency = 30% opacity )
imgdsp.LinePlotImageDisplaySetSliceTransparency( index , 1 , 0.7 )
}
imageDoc.ImageDocumentShow()

Gnuplot, conditional vectorstyle gives an error

I have a comma-separated text file like this:
1386708463,830
1386708473,830
1386708484,830
1386708497,-830
1386708508,-830
1386708518,-840
1386708528,-840
1386708538,-840
1386708686,-950
1386708696,-960
1386708706,-960
1386708716,-940
1386708726,-940
When the value in column 2 is negative the color must be red otherwise green.
I tried with:
plot "< tail -10 meterstanden.txt" using (-100):1:2:0 title "", \
"< tail -11 meterstanden.txt" using (0):1:($2>0 ? $2:0/0):(0) title "Watt/uur " with vectors arrowstyle 2, \
"< tail -11 meterstanden.txt" using (0):1:($2<=0 ? $2:0/0):(0) title "Watt/uur " with vectors arrowstyle 1, \
"< tail -11 meterstanden.txt" using 2:1:2 with labels font "arial, 8" offset 1.5,0.4
But I get this error:
plot "< tail -10 meterstanden.txt" using (-100):1:2:0 title "", "< tail -11 meterstanden.txt" using (0):1:($2>0 ? $2:0/0):(0) title "Watt/uur " with vectors arrowstyle 2, "< tail -11 meterstanden.txt" using (0):1:($2<=0 ? $2:0/0):(0) title "Watt/uur " with vectors arrowstyle 1, "< tail -11 meterstanden.txt" using 2:1:2 with labels font "arial, 8" offset 1.5,0.4
^
"figure02_temp.plt", line 37: warning: Skipping data file with no valid points
I can not figure out what the problem is, what am I doing wrong?
The complete script is:
set output "gnu.png"
set datafile separator ","
set style arrow 1 lw 3 lc rgb "#ff0000"
set style arrow 2 lw 3 lc rgb "#008000"
set style arrow 1 head size screen 0.02,90 # 0.02 is de breedte van het streepje, 90 is een platte streep.
set style arrow 2 head size screen 0.02,90 # 0.02 is de breedte van het streepje, 90 is een platte streep.
set linetype 1 lw 1 pointtype 0 lc rgb"#ff0000"
set bmargin 4 # witruimte onder grafiek
set label font "arial, 8" # grootte font tbv labels in het grafiek
set terminal png notransparent truecolor enhanced
set term png size 500, 450 background rgb "#ffffff"
set ydata time
set timefmt "%s"
set format y "%H:%M:%S" # dit is de opmaak zoals je hem gaat zien
#set key outside bot center
#set key maxrows 1 # aantal regels onder het grafiek (met Watt/uur erin)
set title "Energiestroom" font "arial bold, 14"
set xtics font "arial, 10"
set ytics font "arial, 10"
set ylabel "T i j d - a s" offset 3,1 font "helvetica bold, 14"
set xlabel "W a t t / u u r" offset 0,0.5 font "arial bold, 14"
set grid xtics lc rgb "#dddddd" linewidth 2 lt 1
set grid ytics
set boxwidth 10
set style fill transparent solid 1
unset key
plot "< tail -10 meterstanden.csv" u (-100):1:2:0 title "", \
"< tail -8 meterstanden.csv" u (0):1:($2<0 ?$2:0/0):(0) notitle with vectors arrowstyle 1, \
"< tail -8 meterstanden.csv" u (0):1:($2>0 ?$2:0/0):(0) notitle with vectors arrowstyle 1, \
"< tail -8 meterstanden.csv" u 2:1:2 with labels font "arial, 8" offset 1.5,0.4
The plot is;
http://ccvd.eu/downloads/gnu.png
And after the plot I get the error.
I am VERY glad with your solution. Linux gives me a mail when Gnuplot gives
the trouble. And now thats over, Thanks very much.
And here are the results, a horizontal "histogram" . . . with red and green.
http://ccvd.eu/downloads/gnu1.png
That is a warning, not an error. The reason is, that the last 8 lines of the file (assuming, the data you show is the file meterstanden.csv) all have negative value. Therefore, the third line in your plot, which is supposed to plot the positive arrows, has only 0/0 values. But the plot is correct.
In your case you can use the arrowstyle variable option, which selects the arrow style based on the last column in the using statement. That is easier and you don't get any warnings:
plot "< tail -10 meterstanden.csv" u (-100):1:2:0 title "", \
"< tail -8 meterstanden.csv" u (0):1:2:(0):($2 < 0 ? 1 : 2) notitle with vectors arrowstyle variable,\
"< tail -8 meterstanden.csv" u 2:1:2 with labels font "arial, 8" offset 1.5,0.4