How to make Horizontal Bar in pygal with labels on y? - pygal

Something like this, but without legend and with labels on y.

Here is what I've used to make a horizontal bar with pygal:
bar_chart = pygal.HorizontalBar(show_legend=False)
bar_chart.title = "Title"
bar_chart.add("", [1, 4, 6, 7, 2, 3])
bar_chart.x_labels = ("the", "quick", "brown", "fox", "jumps", "over")
It produced a graph like this:

Related

dc.js charts, how to get the intersection of multiple filters

I am wondering if anyone can help me with getting the intersection of multiple filters.
For example:
if I have following:
var ndx = crossfilter([
{id: 1, arrayVals: [1, 2]},
{id: 2, arrayVals: [2, 3]},
{id: 3, arrayVals: [1, 2, 3]}
]);
how do I get items with arrayVals that have 1 and 3? And how do I get the filter value onclick?
As far as I know, within a dc chart the filtering is "OR" and between dc charts, the filtering is "AND".
Thanks Millions,
Anney

How can I change the font color using axlsx for a cell containing a number?

Here is a test script to create a simple workbook using the axlsx gem. I am trying to loop over the rows and cells and change the font colors to red. However, only the cells that contain text (i.e. the first two rows in this example) are changing to red. The rows containing numbers are remaining the default black color. Any idea how to change the color of cells which contain numbers?
require 'axlsx'
Axlsx::Package.new do |p|
p.workbook.add_worksheet(:name => "test") do |sheet|
sheet.add_row ["First", "Second", "Third"]
sheet.add_row ["First", "Second", "Third"]
sheet.add_row [1, 2, 3]
sheet.add_row [1, 2, 3]
sheet.add_row [1, 2, 3]
sheet.add_row [1, 2, 3]
sheet.add_row [1, 2, 3]
sheet.rows.each do |x|
x.cells.each do |i|
i.color = "E8000"
end
end
end
p.serialize('test.xlsx')
end

Is there a standard way to choose tick locations for an axis in a plot?

When marking tick locations on a plot, are there any standard solutions to how to place the tick markers? I looked at Matplotlib's MaxNLocator (https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/ticker.py#L1212) but it's not immediately clear what all the different options do, or which of them are necessary for basic tick placement.
Can someone provide pseudocode for a simple tick location function?
I think the rule of thumb for placing ticks on a plot is to use multiples of 1, 2, 5, and 10. In my experience, matplotlib seems to abide by this. If you have reason to deviate from the default ticks, I think the easiest way to set them is to use the set_ticks() method for a particular axis. The relevant documentation is here: http://matplotlib.org/api/axis_api.html.
Example
import numpy as np
import matplotlib.pyplot as plt
ax = plt.subplot() # create axes to plot into
foo = np.array([0, 4, 12, 13, 18, 22]) # awkwardly spaced data
bar = np.random.rand(6) # random bar heights
plt.bar(foo, bar) # bar chart
ax.xaxis.get_ticklocs() # check tick locations -- currently array([ 0., 5., 10., 15., 20., 25.])
ax.xaxis.set_ticks(foo) # set the ticks to be right at each bar
ax.xaxis.get_ticklocs() # array([ 0, 4, 12, 13, 18, 22])
plt.draw()
ax.xaxis.set_ticks([0, 10, 20]) # minimal set of ticks
ax.xaxis.get_ticklocs() # array([ 0, 10, 20])
plt.draw()
Of the three options in my example, I would keep the default behaviour in this case; but there are definitely times when I would override the defaults. For example, another rule of thumb is that we should minimize the amount of ink in our plots that is not data (i.e. markers and lines). So if the default tick set was [0, 1, 2, 3, 4, 5, 6], I might change that to [0, 2, 4, 6], since that's less ink for the plot ticks without losing clarity.
Edit: The ticks at [0, 10, 20] can also be accomplished with locators, as suggested in the comment. Examples:
ax.xaxis.set_major_locator(plt.FixedLocator([0,10,20]))
ax.xaxis.set_major_locator(plt.MultipleLocator(base=10))
ax.xaxis.set_major_locator(plt.MaxNLocator(nbins=3))

Gruff is not working well. What to do?

I`m facing a problem with Gruff and Rails.
Examples on the site fail with:
ZeroDivisionError: divided by 0
from /home/prikha/.rvm/gems/ruby-1.9.3-p194#rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:1066:in `label'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194#rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:590:in `setup_graph_measurements'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194#rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:532:in `setup_drawing'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194#rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:508:in `draw'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194#rubymine/gems/gruff-0.3.6/lib/gruff/line.rb:53:in `draw'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194#rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:487:in `write'
from (irb):8
from /home/prikha/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
I had the same problem.
The way i solve it is the following:
ZeroDivisionError appears in 1066 line of gruff/base.rb
Let's look at this file closer
label = if (#spread.to_f % #marker_count.to_f == 0) || !#y_axis_increment.nil?
So, ZeroDivisionError was caused by the fact that #marker_count property was equal to zero. I know, it's not the best solution, but i've added an explicit assignment
#marker_count = <non-zero value> before drawing graph.
So, now example from site looks like:
#!/usr/bin/ruby
require 'rubygems'
require 'gruff'
g = Gruff::Line.new
g.title = "My Graph"
g.data("Apples", [1, 2, 3, 4, 4, 3])
g.data("Oranges", [4, 8, 7, 9, 8, 9])
g.data("Watermelon", [2, 3, 1, 5, 6, 8])
g.data("Peaches", [9, 9, 10, 8, 7, 9])
g.marker_count = 4 #explicitly assign value to #marker_count
g.labels = {0 => '2003', 2 => '2004', 4 => '2005'}
g.write('my_fruity_graph.png')
It works fine for me. I know that it's not the general solution for the problem, but this hack can help you to deal with that library up until this error will be fixed by developers.
ADD
#market_count is a count of markers on vertical axis. So you can play with this property to prettify your graph.

Create an Array of Fixed Length Given an Array of Arbitrary Length

I'm looking to write a method that creates an array of a fixed length (in my case 12) from any array it is provided of arbitrary length (though the length will always be 12 or less) by repeating the objects in order.
So for example given the array a:
a = [1, 2, 3, 4]
I would want to have returned:
a = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Another example:
b = ["peach", "plumb", "pear", "apple", "banana", "orange"]
Would return:
b = ["peach", "plumb", "pear", "apple", "banana", "orange", "peach", "plumb", "pear", "apple", "banana", "orange"]
And so on. If given an array with 12 objects, it would just return the same array.
The methods I've written to accomplish this so far have been very ugly and not very Rubyish; interested in how others would handle this.
Thanks in advance.
In 1.8.7 & 1.9 you can do cool stuff with Enumerators
a = [1,2,3,4]
#=> [1,2,3,4]
a.cycle.take 12
#=> [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Array.new(12).fill(some_array).flatten[0..11]
def twelvify array
array += array while array.size < 12
array[0..11]
end
It's also a bit ugly but it's, at least, simple. :-)
array * (12/array.size) + array[0, (12 % array.size)]

Resources