I have a table as below:
data = {'Platform':['Server A', 'Server A', 'Server A', 'Server A', 'Server A', 'Server A', 'Server B', 'Server B', 'Server B', 'Server B', 'Server B', 'Server B', 'Server C', 'Server C', 'Server C', 'Server C', 'Server C', 'Server C', 'Server C', 'Server C', 'Server C', 'Server D', 'Server D', 'Server D', 'Server D', 'Server D', 'Server D', 'Server D', 'Server D', 'Server D'],
'Grade':['Excellent', 'Average', 'Excellent', 'Average', 'Excellent', 'Average','Excellent', 'Average', 'Excellent', 'Average', 'Excellent', 'Average', 'Excellent', 'Excellent', 'Excellent', 'Average', 'Average', 'Average', 'Bad', 'Bad', 'Bad', 'Excellent', 'Excellent', 'Excellent', 'Average', 'Average', 'Average', 'Bad', 'Bad', 'Bad'],
'Colour':['Yellow', 'Yellow', 'Green', 'Green', 'Black', 'Black', 'Yellow', 'Yellow', 'Green', 'Green', 'Black', 'Black', 'Yellow', 'Green', 'Black', 'Yellow', 'Green', 'Black', 'Yellow', 'Green', 'Black', 'Yellow', 'Green', 'Black', 'Yellow', 'Green', 'Black', 'Yellow', 'Green', 'Black'],
'Count':[4115314,3879421,4240053,4019764,5398596,5019698,4652935,4140395,4786148,4306763,5691699,5181920,4046690,4202843,5320250,3772534,3945649,4936451,4242814,4490521,5341960,4926092,5095662,5816803,4404762,4587462,5298671,4948988,5146153,5720155]
}
df = pd.DataFrame(data, columns = ['Platform', 'Grade', 'Colour', 'Count'])
And I am looking to create a FacetGrid containing bar plots for the Counts for each Platform, Grade, and Colour.
g = sns.FacetGrid(df, col="Platform")
g.map_dataframe(sns.barplot, x="Grade", y="Counts", hue="Colour")
g.set_axis_labels("Grade", "Counts")
g.add_legend()
g.set_titles(col_template="{col_name}", row_template="{row_name}")
I am able to get a FacetGrid fine with this code. However, how do I add labels for Counts at the top of each bar?
I would use a custom function passed to g.map():
def annotate_bars(ax=None, fmt='.2f', **kwargs):
ax = plt.gca() if ax is None else ax
for p in ax.patches:
ax.annotate('{{:{:s}}}'.format(fmt).format(p.get_height()), (p.get_x() + p.get_width() / 2., p.get_height()),
xytext=(0, 5),textcoords='offset points',
ha='center', va='center', **kwargs)
df = pd.DataFrame(data)
g = sns.FacetGrid(df, col="Platform")
g.map_dataframe(sns.barplot, x="Grade", y="Counts", hue="Colour")
g.map(annotate_bars, fmt='.2g', fontsize=8, color='k')
g.set_axis_labels("Grade", "Counts")
g.add_legend()
g.set_titles(col_template="{col_name}", row_template="{row_name}")
Related
I try to use the following code for my application to display the weight of animals in a chart. My goal is a variable time controlled via buttons (e.g. 1 month, 6 months, 1 year,...).
LineChart(
LineChartData(
lineTouchData: LineTouchData(enabled: true),
gridData: FlGridData(
show: false,
drawHorizontalLine: false,
drawVerticalLine: false,
getDrawingVerticalLine: (value) {
return FlLine(
color: Colors.black,
strokeWidth: 1,
);
},
getDrawingHorizontalLine: (value) {
return FlLine(
color: const Color(0xff37434d),
strokeWidth: 1,
);
},
),
lineBarsData: [
LineChartBarData(
spots: [
FlSpot(0, data_snap()['Historie_Inhalt']['0']),
FlSpot(1, data_snap()['Historie_Inhalt']['1']),
FlSpot(2, data_snap()['Historie_Inhalt']['2']),
FlSpot(3, data_snap()['Historie_Inhalt']['3']),
FlSpot(4, data_snap()['Historie_Inhalt']['4']),
FlSpot(6, data_snap()['Historie_Inhalt']['5'])],
isCurved: true,
barWidth: 1,
colors: [
Colors.black87,
],
belowBarData: BarAreaData(
show: true,
),
aboveBarData: BarAreaData(
show: true,
colors: [Colors.grey.withOpacity(0.3)]
),
dotData: FlDotData(
show: false,
getDotPainter: (spot, percent, barData, index) =>
FlDotCirclePainter(
radius: 10,
color: Colors.deepOrange.withOpacity(0.5)),
),
),
],
titlesData: FlTitlesData(
bottomTitles: SideTitles(
showTitles: false,
reservedSize: 10,
getTitles: (value) {
switch (value.toInt()) {
case 0:
return '2019';
case 1:
return '2020';
case 2:
return '2021';
case 3:
return '2022';
default:
return '';
}
}),
leftTitles: SideTitles(showTitles: true, margin: 3,
reservedSize: SizeConfig.safeBlockHorizontal * 11
),
rightTitles: SideTitles(showTitles: false),
topTitles: SideTitles(showTitles: false),
),
),
swapAnimationDuration: Duration(milliseconds: 1500),
)
I get the following error directly for the first FlSpot:
type 'String' is not a subtype of type 'int' of 'index'
So far I retrieved only six fixed values from Firebase and displayed them with the following code:
FlSpot(5, data_snap()['P_1W_W']),
'P_1W_W' is in the screenshot below on the same level as 'name'.
The database looks like this ('Historie_Inhalt' is defined as an Array):
simple multi barchart only shows the main category ['one', 'two', 'three', 'four', 'five', 'six'] as the x axis labels. Is there a way to show subcategory ['A', 'B', 'C', 'D'] as secondary x axis labels in chartjs?
the graph with sub category labels
You can use grouped bar chart, like here:
const datasets = [{
label: "red",
backgroundColor: "red",
data: [33, 91, null, 48]
}, {
label: "blue",
backgroundColor: "blue",
data: [38, 57, 75, 84]
},
{
label: "yellow",
backgroundColor: "yellow",
data: [97, null, 67, 41]
}
];
new Chart(document.getElementById("myChart"), {
type: 'bar',
data: {
labels: ["one", "two", "three", "four"],
datasets
},
options: {
title: {
display: true,
text: 'Grouped bar chart'
},
scales: {
xAxes: [{
ticks: {
callback: function(label, index, labels, chart) {
let result = "" // initialize
datasets.forEach((dataset) => {
if (dataset.data[index] !== null) {
result += (result.length > 0 ? ', ' : '') + dataset.label;
}
})
return result
}
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="200"></canvas>
Here you can find more options and explanation: https://tobiasahlin.com/blog/chartjs-charts-to-get-you-started/
I'm attempting to produce a graph using graphviz that produces a visualisation like this fantastic one(!; https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwjcgcDdvcHjAhXG854KHaMHB2EQjRx6BAgBEAU&url=https%3A%2F%2Fwww.kathartman.com%2Fvisualizing-detroits-civic-tech-ecosystem&psig=AOvVaw1P66IWi0HPrMZQMmEP_MuS&ust=1563642864370970). My current strategy is to produce an outer circle of nodes (achieved), and now want to add the nodes to the centre of the visualisation (unsuccessful thus far!).
I've explored subgraph clusters, which seems the cleanest solution, but haven't found a way to change the layout from the circo layout. I've also explored merging two different graphs but haven't discovered a way to do this as yet. I've also tried other r packages (E.g. visnetwork, networkd3, circlize) with diagrammeR/graphviz seeming the most obvious solution again...
grViz("graph {
graph [bgcolor = 'white',layout = 'circo',
outputorder = 'edgesfirst']
subgraph cluster0 {
node [shape = 'rectangle',fontsize = 22,fontname = 'Helvetica',
fontcolor = 'black', width = 3, height = 1.25, style = 'dotted',
penwidth = 5]
'1'
'2'
'3'
node [shape = 'plaintext',fontsize = 16,fontname = 'Helvetica',
width = 1, height = 1]
'18'
'5'
node [shape = 'plaintext',fontsize = 16, fontname = 'Helvetica',
fontcolor = 'black']
'4'
'9'
'6'
node [fillcolor = OldLace,
fontsize = 11,fontsize = 14]
'7'
'8'
'21'
'22'
'23'
'10'
'20'
edge [weight = 0.5,penwidth = 4, fontname = 'Helvetica',
fontsize = '12', len = '1.5', color = 'gray80',
arrowsize = '0.5']
'1'--'18'
'18'--'4'
'4'--'7'
'7'--'8'
'8'--'2'
'2'--'5'
'5'--'9'
'9'--'21'
'21'--'22'
'22'--'23'
'23'--'3'
'3'--'6'
'6'--'10'
'10'--'20'
'20'--'1'}
subgraph cluster1 {
# Projects
node [fontname = 'Helvetica',
fontsize = '7', shape = 'circle',
fixedsize = 'true', width = '0.5',
style = 'filled', fillcolor = 'aliceblue',
fontcolor = 'gray50']
'11'
'12'
'13'
'14'
'15'
'16'
'17'
'19'
edge [weight = 0.25,fontname = 'Helvetica',
fontsize = '12', len = '1.5', color = 'gray80',
arrowsize = '0.5']
'6'--{'10' '17' '19'}
'7'--{'11' '19'}
'8'--{'12' '13' '14' '15'}
'9'--{'12' '13' '14' '16'}
}
}")
The goal is that the second subcluster (or 2nd graph) forms a network within the first subcluster, while still displaying the edges between. Currently, the 2nd subcluster populates the outside of the circle and remains in a circo layout
When running my node.js project locally on my computer, moment.js is showing correct time (2019-10-28T07:00:00.000Z because moment have adjusted for DST +02:00 in april and +01:00 in october)):
Moment {
_isAMomentObject: true,
_i: '2019-04-15T06:00:00.000Z',
_f: 'YYYY-MM-DDTHH:mm:ss.SSSSZ',
_tzm: 0,
_isUTC: false,
_pf:
{ empty: false,
unusedTokens: [],
unusedInput: [],
overflow: -1,
charsLeftOver: 0,
nullInput: false,
invalidMonth: null,
invalidFormat: false,
userInvalidated: false,
iso: true,
parsedDateParts: [ 2019, 3, 15, 6, 0, 0, 0 ],
meridiem: undefined,
rfc2822: false,
weekdayMismatch: false },
_locale:
Locale {
_calendar:
{ sameDay: '[Today at] LT',
nextDay: '[Tomorrow at] LT',
nextWeek: 'dddd [at] LT',
lastDay: '[Yesterday at] LT',
lastWeek: '[Last] dddd [at] LT',
sameElse: 'L' },
_longDateFormat:
{ LTS: 'h:mm:ss A',
LT: 'h:mm A',
L: 'MM/DD/YYYY',
LL: 'MMMM D, YYYY',
LLL: 'MMMM D, YYYY h:mm A',
LLLL: 'dddd, MMMM D, YYYY h:mm A' },
_invalidDate: 'Invalid date',
ordinal: [Function: ordinal],
_dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/,
_relativeTime:
{ future: 'in %s',
past: '%s ago',
s: 'a few seconds',
ss: '%d seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years' },
_months:
[ 'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December' ],
_monthsShort:
[ 'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec' ],
_week: { dow: 0, doy: 6 },
_weekdays:
[ 'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday' ],
_weekdaysMin: [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ],
_weekdaysShort: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
_meridiemParse: /[ap]\.?m?\.?/i,
_abbr: 'en',
_config:
{ calendar: [Object],
longDateFormat: [Object],
invalidDate: 'Invalid date',
ordinal: [Function: ordinal],
dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/,
relativeTime: [Object],
months: [Array],
monthsShort: [Array],
week: [Object],
weekdays: [Array],
weekdaysMin: [Array],
weekdaysShort: [Array],
meridiemParse: /[ap]\.?m?\.?/i,
abbr: 'en' },
_dayOfMonthOrdinalParseLenient: /\d{1,2}(th|st|nd|rd)|\d{1,2}/ },
_a: [ 2019, 3, 15, 6, 0, 0, 0 ],
_d: 2019-10-28T07:00:00.000Z,
_isValid: true,
_z: null }
But on my server, I get this:
Moment {
_isAMomentObject: true,
_i: '2019-04-15T06:00:00.000Z',
_f: 'YYYY-MM-DDTHH:mm:ss.SSSSZ',
week: [Object],
weekdays: [Array],
weekdaysMin: [Array],
weekdaysShort: [Array],
meridiemParse: /[ap]\.?m?\.?/i,
abbr: 'en' },
_dayOfMonthOrdinalParseLenient: /\d{1,2}(th|st|nd|rd)|\d{1,2}/ },
_a: [ 2019, 3, 15, 6, 0, 0, 0 ],
_d: 2019-10-28T06:00:00.000Z,
_isValid: true,
_z: null }
The date is incorrectly set to 2019-10-28T06:00:00:000Z.
I have set the timezone on the server with sudo timedatectl set-timezone Europe/Oslo
If I am using moment.isDST() for dates on the server, it correctly gives my true in April and false in October.
I am unsure how to solve this, on the server or in my code?
I think I solved it. By answering the from Styx questions, I finally realized that the server app running in a docker container didn't have the correct timezone. By setting environment: TZ: "Europe/Oslo" in the docker-compose.yml file this fixed the problem, and I am getting correct time now.
How could I achieve the chart below as accurate as possible?
I'm trying to achieve the chart in the picture below with highcharts, the problem I have is that I can't achieve the gradients and the purple cut-line
this is what I have donde so far : jsFiddle
$(function () {
$('#container').highcharts({
chart: {
type: 'areaspline'
},
options: {
title: {
text: "Historical report"
},
heigth: 200
},
legend: {
layout: 'vertical',
align: 'left',
verticalAlign: 'top',
x: 150,
y: 100,
floating: true,
borderWidth: 1,
backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
},
xAxis: {
categories: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
plotBands: [
{
from: 4.5,
to: 6.5,
color: 'rgba(68, 170, 213, .2)'
}
]
},
yAxis: {
title: {
text: 'Fruit units'
}
},
tooltip: {
shared: true,
valueSuffix: ' units'
},
credits: {
enabled: false
},
plotOptions: {
areaspline: {
fillOpacity: 0.5
}
},
series: [
{
name: 'John',
data: [3, 9, null, 5, 4, 10, 12],
lineColor: "#5A66AF"
}, {
name: 'Jane',
data: [1, 10, null, 3, 3, 5, 4],
lineColor: '#47a06b'
}, {
name: 'Roberto',
data: [10, 15, null, 15, 9, 9, 4],
lineColor: '#2ba9db'
}
]
});
});
The line is achieved by the DashStyle property:
http://api.highcharts.com/highcharts#plotOptions.line.dashStyle
The gradient fill is a matter of defining the gradient stops in the fillColor property:
http://api.highcharts.com/highcharts#plotOptions.area.fillColor
http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/plotoptions/area-fillcolor-gradient/
(though, FWIW, that extreme white end to the gradient is reeeeally distracting...)