example of pareto chart

Here is my example of a Pareto chart.

For an explanation of a Pareto chart:

http://en.wikipedia.org/wiki/Pareto_chart

Could I get this chart added to the matplolib gallery?

Thanks

Paul

import matplotlib.pyplot as plt
import numpy as np

def update_ax2(axx):
ax2.set_ylim(0, 100)
ax2.figure.canvas.draw()

the data to plot

defects = [32, 22, 15, 5, 2]
labels = [‘vertical’, ‘horizontal’, ‘behind’, ‘left area’, ‘other’]
the_sum = sum(defects) # ie, 32 + 22 + 15 + 5 + 2
the_cumsum = np.cumsum(defects) # 32, 32 + 22, 32 + 22 + 15, 32 + 22 + 15 + 5, 32 + 22, + 15 + 5 + 2
ind = np.arange(len(defects)) # the x locations for the groups
width = .98 # with do of the bars, where a width of 1 indidcates no space between bars
x = ind + .5 * width # find the middle of the bar
fig = plt.figure() # create a figure
ax1 = fig.add_subplot(111) # and a subplot
ax2 = ax1.twinx() # create a duplicate y axis

create the callback to automatically update the y axis

ax1.callbacks.connect(“ylim_changed”, update_ax2)

create an upper limit for the y axis.

The upper limit is the sum of all the numbers

ax1.set_ylim(ymax=the_sum)
rects1 = ax1.bar(ind, defects, width=width) # draw the chart
line, = ax1.plot(x, the_cumsum) # draw the line
ax1.set_xticks(ind+ .5 * width) # set ticks for middle of bars
ax1.set_xticklabels(labels) # create the labels for the bars
ax1.set_ylabel(‘Defects’) # create the left y axis label
ax2.set_ylabel(‘Percentage’) # create the right y axis label
plt.show()

Your code looks overly complicated. You shouldn’t have to be doing the connection to the ylim_changed event, I don’t think. I think your main problem is that you are calling ax1.plot instead of ax2.plot.

I am not against adding more examples to the gallery, but this would have to be cleaned up before it gets included.

Ben Root

···

On Mon, Sep 24, 2012 at 12:21 AM, Paul Tremblay <paulhtremblay@…287…> wrote:

Here is my example of a Pareto chart.

For an explanation of a Pareto chart:

http://en.wikipedia.org/wiki/Pareto_chart

Could I get this chart added to the matplolib gallery?

Thanks

Paul

I took my example from the matplotlib pages itself:

http://matplotlib.org/examples/api/fahrenheit_celcius_scales.html

If you know a better way, please show me.

P.

···

On 9/24/12 4:40 PM, Benjamin Root wrote:

On Mon, Sep 24, 2012 at 12:21 AM, Paul Tremblay > <paulhtremblay@…287… <mailto:paulhtremblay@…287…>> wrote:

    Here is my example of a Pareto chart.

    For an explanation of a Pareto chart:

    http://en.wikipedia.org/wiki/Pareto_chart

    Could I get this chart added to the matplolib gallery?

    Thanks

    Paul

Your code looks overly complicated. You shouldn't have to be doing the connection to the ylim_changed event, I don't think. I think your main problem is that you are calling ax1.plot instead of ax2.plot.

I am not against adding more examples to the gallery, but this would have to be cleaned up before it gets included.

Ben Root

By the way, I had done the chart differently to begin with. But this code requires more lines, more imports, and is more complex. (Without plt.gca().yaxis or the formatter, the graph will not come out.)

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter
from matplotlib.ticker import MaxNLocator
defects = [32, 22, 15, 5, 2]
labels = [‘vertical’, ‘horizontal’, ‘behind’, ‘left area’, ‘other’]
the_sum = sum(defects)
the_cumsum = np.cumsum(defects)
ind = np.arange(len(defects))
width = .98
x = ind + .5 * width
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
rects = ax1.bar(ind, defects, width=width)
ax1.set_ylim(ymax=the_sum)
ax2.set_ylim(ymax=the_sum)
plt.gca().yaxis.set_major_locator( MaxNLocator(nbins = 6) )
line, = ax2.plot(x, the_cumsum)
ax1.set_xticks(ind+ .5 * width)
ax1.set_xticklabels(labels)
def to_percent(x, pos):
return round(x/the_sum, 1) * 100
formatter = FuncFormatter(to_percent)
ax2.yaxis.set_major_formatter(formatter)
ax1.set_ylabel(‘Defects’)
ax2.set_ylabel(‘Percentage’)
plt.show()

···

On Mon, Sep 24, 2012 at 8:50 PM, Paul Tremblay <paulhtremblay@…287…> wrote:

  I took my example from the matplotlib

pages itself:

  [http://matplotlib.org/examples/api/fahrenheit_celcius_scales.html](http://matplotlib.org/examples/api/fahrenheit_celcius_scales.html)



  If you know a better way, please show me.



  P.




  On 9/24/12 4:40 PM, Benjamin Root wrote:
    On Mon, Sep 24, 2012 at 12:21 AM, Paul > > Tremblay <paulhtremblay@...287...> > >         wrote:

Here is my example of a Pareto chart.

      For an explanation of a Pareto chart:



      [http://en.wikipedia.org/wiki/Pareto_chart](http://en.wikipedia.org/wiki/Pareto_chart)



      Could I get this chart added to the matplolib gallery?





      Thanks



      Paul
      Your code looks overly complicated.  You shouldn't have to be

doing the connection to the ylim_changed event, I don’t
think. I think your main problem is that you are calling
ax1.plot instead of ax2.plot.

      I am not against adding more examples to the gallery, but this

would have to be cleaned up before it gets included.

      Ben Root

Paul,

That example is an overly complicated template for making a pareto chart.

Here's how I'd do it:

# data
defects = [0, 32, 22, 15, 5, 2]
labels = ['', 'vertical', 'horizontal', 'behind', 'left area', 'other']

# axes
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

# plotting
ax1.bar(np.arange(len(defects))-0.4, defects, zorder=0, alpha=0.5)
ax2.plot(np.cumsum(defects), linestyle='-', color='k', linewidth=2, zorder=5)

# formatting
ax1.set_xticks(np.arange(len(defects)))
ax1.set_xticklabels(labels)
ax1.set_ylabel('Defects')
ax2.set_ylabel('Percentage')
plt.show()

···

On Mon, Sep 24, 2012 at 12:21 AM, Paul Tremblay <paulhtremblay@...287...> > wrote:

Here is my example of a Pareto chart.

For an explanation of a Pareto chart:

http://en.wikipedia.org/wiki/Pareto_chart

Could I get this chart added to the matplolib gallery?

Thanks

Paul

On 9/24/12 4:40 PM, Benjamin Root wrote:
Your code looks overly complicated. You shouldn't have to be doing the
connection to the ylim_changed event, I don't think. I think your main
problem is that you are calling ax1.plot instead of ax2.plot.

I am not against adding more examples to the gallery, but this would have to
be cleaned up before it gets included.

Ben Root

On Mon, Sep 24, 2012 at 5:50 PM, Paul Tremblay <paulhtremblay@...287...> wrote:

I took my example from the matplotlib pages itself:

http://matplotlib.org/examples/api/fahrenheit_celcius_scales.html

If you know a better way, please show me.

P.

I think pareto charts are supposed to be percentages, not totals.

data = [83, 38, 7, 5, 5, 4, 4, 2, 1]
labels = ["Vertical", "Horizontal", "Upper", "Lower", "Left", "Right",
"Behind", "Front", "Down"]
colors = ["#001499", "#ff7f00", "#9440ed", "#edc240", "#238c3f",
"#a60085", "#00cca3", "#464f8c", "#005947", "#00004d",
        "#cc0052", "#591616", "#7d8060", "#299da6", "#9c8fbf",
"#4c132a", "#8c3f23", "#85cc33", "#607980", "#7c30bf", "#bf9360",
        "#324d13", "#13394d", "#4c1659", "#b25f00", "#99cca7",
"#669ccc", "#594358"]

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

for i, d in enumerate(data):
    ax1.bar(i + .25, d, .5, zorder=0, alpha=0.5, label = labels[i],
color=colors[i % len(colors)])

percent = [d*1.0/sum(data) * 100 for d in np.cumsum(data)]
ax2.plot(np.arange(len(data)) + 0.5, percent, linestyle='-',
color='k', linewidth=2, zorder=5)

ax1.set_xticks(np.arange(len(data)) + 0.5)
ax1.set_xticklabels(labels, rotation=30, ha='right')
#ax1.legend()

ax1.set_ylabel('Defects')
ax2.set_ylabel('Percentage')
ax2.set_ylim(0,110)

plt.show()

···

On Mon, Sep 24, 2012 at 11:43 PM, Paul Hobson <pmhobson@...287...> wrote:

On Mon, Sep 24, 2012 at 12:21 AM, Paul Tremblay <paulhtremblay@...287...> >> wrote:

Here is my example of a Pareto chart.

For an explanation of a Pareto chart:

http://en.wikipedia.org/wiki/Pareto_chart

Could I get this chart added to the matplolib gallery?

Thanks

Paul

On 9/24/12 4:40 PM, Benjamin Root wrote:
Your code looks overly complicated. You shouldn't have to be doing the
connection to the ylim_changed event, I don't think. I think your main
problem is that you are calling ax1.plot instead of ax2.plot.

I am not against adding more examples to the gallery, but this would have to
be cleaned up before it gets included.

Ben Root

On Mon, Sep 24, 2012 at 5:50 PM, Paul Tremblay <paulhtremblay@...287...> wrote:

I took my example from the matplotlib pages itself:

http://matplotlib.org/examples/api/fahrenheit_celcius_scales.html

If you know a better way, please show me.

P.

Paul,

That example is an overly complicated template for making a pareto chart.

Here's how I'd do it:

# data
defects = [0, 32, 22, 15, 5, 2]
labels = ['', 'vertical', 'horizontal', 'behind', 'left area', 'other']

# axes
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

# plotting
ax1.bar(np.arange(len(defects))-0.4, defects, zorder=0, alpha=0.5)
ax2.plot(np.cumsum(defects), linestyle='-', color='k', linewidth=2, zorder=5)

# formatting
ax1.set_xticks(np.arange(len(defects)))
ax1.set_xticklabels(labels)
ax1.set_ylabel('Defects')
ax2.set_ylabel('Percentage')
plt.show()

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

There are two problems with this chart:

  1. The scale is wrong. Imagine that you can stack all the bars on top of each other. When stacked, all the bars should fill in the graph exactly. In other words: ax1.set_ylim = sum(defects). See my original, or the wiki page.

  2. The line starts in the middle of the bar. It needs to start on top, in the middle.

Maybe there is a way to correct 1 & 2 with your method, but I can’t seem to find it.

Thanks!

Paul

···

On Tue, Sep 25, 2012 at 12:58 PM, Jeffrey Melloy <jmelloy@…287…> wrote:

I think pareto charts are supposed to be percentages, not totals.

data = [83, 38, 7, 5, 5, 4, 4, 2, 1]

labels = [“Vertical”, “Horizontal”, “Upper”, “Lower”, “Left”, “Right”,

“Behind”, “Front”, “Down”]

colors = ["#001499", “#ff7f00”, “#9440ed”, “#edc240”, “#238c3f”,

#a60085”, “#00cca3”, “#464f8c”, “#005947”, “#00004d”,

    "#cc0052", "#591616", "#7d8060", "#299da6", "#9c8fbf",

#4c132a”, “#8c3f23”, “#85cc33”, “#607980”, “#7c30bf”, “#bf9360”,

    "#324d13", "#13394d", "#4c1659", "#b25f00", "#99cca7",

#669ccc”, “#594358”]

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()

for i, d in enumerate(data):

ax1.bar(i + .25, d, .5, zorder=0, alpha=0.5, label = labels[i],

color=colors[i % len(colors)])

percent = [d*1.0/sum(data) * 100 for d in np.cumsum(data)]

ax2.plot(np.arange(len(data)) + 0.5, percent, linestyle=’-’,
color=‘k’, linewidth=2, zorder=5)

ax1.set_xticks(np.arange(len(data)) + 0.5)

ax1.set_xticklabels(labels, rotation=30, ha=‘right’)

#ax1.legend()

ax1.set_ylabel(‘Defects’)

ax2.set_ylabel(‘Percentage’)

ax2.set_ylim(0,110)

plt.show()

On Mon, Sep 24, 2012 at 11:43 PM, Paul Hobson <pmhobson@…287…> wrote:

On Mon, Sep 24, 2012 at 12:21 AM, Paul Tremblay <paulhtremblay@…287…> > > >> wrote:

Here is my example of a Pareto chart.

For an explanation of a Pareto chart:

http://en.wikipedia.org/wiki/Pareto_chart

Could I get this chart added to the matplolib gallery?

Thanks

Paul

On 9/24/12 4:40 PM, Benjamin Root wrote:

Your code looks overly complicated. You shouldn’t have to be doing the

connection to the ylim_changed event, I don’t think. I think your main

problem is that you are calling ax1.plot instead of ax2.plot.

I am not against adding more examples to the gallery, but this would have to

be cleaned up before it gets included.

Ben Root

On Mon, Sep 24, 2012 at 5:50 PM, Paul Tremblay <paulhtremblay@…287…> wrote:

I took my example from the matplotlib pages itself:

http://matplotlib.org/examples/api/fahrenheit_celcius_scales.html

If you know a better way, please show me.

P.

Paul,

That example is an overly complicated template for making a pareto chart.

Here’s how I’d do it:

data

defects = [0, 32, 22, 15, 5, 2]

labels = [’’, ‘vertical’, ‘horizontal’, ‘behind’, ‘left area’, ‘other’]

axes

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()

plotting

ax1.bar(np.arange(len(defects))-0.4, defects, zorder=0, alpha=0.5)

ax2.plot(np.cumsum(defects), linestyle=’-’, color=‘k’, linewidth=2, zorder=5)

formatting

ax1.set_xticks(np.arange(len(defects)))

ax1.set_xticklabels(labels)

ax1.set_ylabel(‘Defects’)

ax2.set_ylabel(‘Percentage’)

plt.show()


Live Security Virtual Conference

Exclusive live event will cover all the ways today’s security and

threat landscape has changed and how IT managers can respond. Discussions

will include endpoint security, mobile security and the latest in malware

threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

ax1.set_ylim(0, sum(data))
ax2.set_ylim(0, 100)
seems to solve both of these issues.

pareto.png

···

On Tue, Sep 25, 2012 at 11:16 AM, Paul Tremblay <paulhtremblay@...287...> wrote:

There are two problems with this chart:

1. The scale is wrong. Imagine that you can stack all the bars on top of
each other. When stacked, all the bars should fill in the graph exactly. In
other words: ax1.set_ylim = sum(defects). See my original, or the wiki page.

2. The line starts in the middle of the bar. It needs to start on top, in
the middle.

Maybe there is a way to correct 1 & 2 with your method, but I can't seem to
find it.

Thanks!

Paul

On Tue, Sep 25, 2012 at 12:58 PM, Jeffrey Melloy <jmelloy@...287...> wrote:

I think pareto charts are supposed to be percentages, not totals.

data = [83, 38, 7, 5, 5, 4, 4, 2, 1]
labels = ["Vertical", "Horizontal", "Upper", "Lower", "Left", "Right",
"Behind", "Front", "Down"]
colors = ["#001499", "#ff7f00", "#9440ed", "#edc240", "#238c3f",
"#a60085", "#00cca3", "#464f8c", "#005947", "#00004d",
        "#cc0052", "#591616", "#7d8060", "#299da6", "#9c8fbf",
"#4c132a", "#8c3f23", "#85cc33", "#607980", "#7c30bf", "#bf9360",
        "#324d13", "#13394d", "#4c1659", "#b25f00", "#99cca7",
"#669ccc", "#594358"]

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

for i, d in enumerate(data):
    ax1.bar(i + .25, d, .5, zorder=0, alpha=0.5, label = labels[i],
color=colors[i % len(colors)])

percent = [d*1.0/sum(data) * 100 for d in np.cumsum(data)]
ax2.plot(np.arange(len(data)) + 0.5, percent, linestyle='-',
color='k', linewidth=2, zorder=5)

ax1.set_xticks(np.arange(len(data)) + 0.5)
ax1.set_xticklabels(labels, rotation=30, ha='right')
#ax1.legend()

ax1.set_ylabel('Defects')
ax2.set_ylabel('Percentage')
ax2.set_ylim(0,110)

plt.show()

On Mon, Sep 24, 2012 at 11:43 PM, Paul Hobson <pmhobson@...287...> wrote:
>> On Mon, Sep 24, 2012 at 12:21 AM, Paul Tremblay >> >> <paulhtremblay@...287...> >> >> wrote:
>>>
>>> Here is my example of a Pareto chart.
>>>
>>> For an explanation of a Pareto chart:
>>>
>>> http://en.wikipedia.org/wiki/Pareto_chart
>>>
>>> Could I get this chart added to the matplolib gallery?
>>>
>>>
>>> Thanks
>>>
>>> Paul
>>>
>
>> On 9/24/12 4:40 PM, Benjamin Root wrote:
>> Your code looks overly complicated. You shouldn't have to be doing the
>> connection to the ylim_changed event, I don't think. I think your main
>> problem is that you are calling ax1.plot instead of ax2.plot.
>>
>> I am not against adding more examples to the gallery, but this would
>> have to
>> be cleaned up before it gets included.
>>
>> Ben Root
>
> On Mon, Sep 24, 2012 at 5:50 PM, Paul Tremblay <paulhtremblay@...287...> >> > wrote:
>> I took my example from the matplotlib pages itself:
>>
>> http://matplotlib.org/examples/api/fahrenheit_celcius_scales.html
>>
>> If you know a better way, please show me.
>>
>> P.
>
> Paul,
>
> That example is an overly complicated template for making a pareto
> chart.
>
> Here's how I'd do it:
>
> # data
> defects = [0, 32, 22, 15, 5, 2]
> labels = ['', 'vertical', 'horizontal', 'behind', 'left area', 'other']
>
> # axes
> fig, ax1 = plt.subplots()
> ax2 = ax1.twinx()
>
> # plotting
> ax1.bar(np.arange(len(defects))-0.4, defects, zorder=0, alpha=0.5)
> ax2.plot(np.cumsum(defects), linestyle='-', color='k', linewidth=2,
> zorder=5)
>
> # formatting
> ax1.set_xticks(np.arange(len(defects)))
> ax1.set_xticklabels(labels)
> ax1.set_ylabel('Defects')
> ax2.set_ylabel('Percentage')
> plt.show()
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond.
> Discussions
> will include endpoint security, mobile security and the latest in
> malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Yes, that works nice. So my final code, as minimalist as possible (while still maintaining readability):

import matplotlib.pyplot as plt
import numpy as np

the data to plot

defects = [32, 22, 15, 5, 2]
labels = [‘vertical’, ‘horizontal’, ‘behind’, ‘left area’, ‘other’]
the_sum = sum(defects) # ie, 32 + 22 + 15 + 5 + 2
the_cumsum = np.cumsum(defects) # 32, 32 + 22, 32 + 22 + 15, 32 + 22 + 15 + 5, 32 + 22, + 15 + 5 + 2
percent = (np.divide(the_cumsum, the_sum)) * 100
ind = np.arange(len(defects)) # the x locations for the groups
width = .98 # with do of the bars, where a width of 1 indidcates no space between bars
x = ind + .5 * width # find the middle of the bar
fig = plt.figure() # create a figure
ax1 = fig.add_subplot(111) # and a subplot
ax2 = ax1.twinx() # create a duplicate y axis
rects1 = ax1.bar(ind, defects, width=width) # draw the chart
line, = ax2.plot(x, percent) # draw the line
ax1.set_ylim(ymax=the_sum) # without these limits, graphs will not work
ax2.set_ylim(0, 100)
ax1.set_xticks(x) # set ticks for middle of bars
ax1.set_xticklabels(labels) # create the labels for the bars
ax1.set_ylabel(‘Defects’) # create the left y axis label
ax2.set_ylabel(‘Percentage’) # create the right y axis label
plt.show()

···

On Tue, Sep 25, 2012 at 2:31 PM, Jeffrey Melloy <jmelloy@…287…> wrote:

ax1.set_ylim(0, sum(data))

ax2.set_ylim(0, 100)

seems to solve both of these issues.

On Tue, Sep 25, 2012 at 11:16 AM, Paul Tremblay <paulhtremblay@…287…> wrote:

There are two problems with this chart:

  1. The scale is wrong. Imagine that you can stack all the bars on top of

each other. When stacked, all the bars should fill in the graph exactly. In

other words: ax1.set_ylim = sum(defects). See my original, or the wiki page.

  1. The line starts in the middle of the bar. It needs to start on top, in

the middle.

Maybe there is a way to correct 1 & 2 with your method, but I can’t seem to

find it.

Thanks!

Paul

On Tue, Sep 25, 2012 at 12:58 PM, Jeffrey Melloy <jmelloy@…287…> wrote:

I think pareto charts are supposed to be percentages, not totals.

data = [83, 38, 7, 5, 5, 4, 4, 2, 1]

labels = [“Vertical”, “Horizontal”, “Upper”, “Lower”, “Left”, “Right”,

“Behind”, “Front”, “Down”]

colors = ["#001499", “#ff7f00”, “#9440ed”, “#edc240”, “#238c3f”,

#a60085”, “#00cca3”, “#464f8c”, “#005947”, “#00004d”,

    "#cc0052", "#591616", "#7d8060", "#299da6", "#9c8fbf",

#4c132a”, “#8c3f23”, “#85cc33”, “#607980”, “#7c30bf”, “#bf9360”,

    "#324d13", "#13394d", "#4c1659", "#b25f00", "#99cca7",

#669ccc”, “#594358”]

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()

for i, d in enumerate(data):

ax1.bar(i + .25, d, .5, zorder=0, alpha=0.5, label = labels[i],

color=colors[i % len(colors)])

percent = [d*1.0/sum(data) * 100 for d in np.cumsum(data)]

ax2.plot(np.arange(len(data)) + 0.5, percent, linestyle=’-’,

color=‘k’, linewidth=2, zorder=5)

ax1.set_xticks(np.arange(len(data)) + 0.5)

ax1.set_xticklabels(labels, rotation=30, ha=‘right’)

#ax1.legend()

ax1.set_ylabel(‘Defects’)

ax2.set_ylabel(‘Percentage’)

ax2.set_ylim(0,110)

plt.show()

On Mon, Sep 24, 2012 at 11:43 PM, Paul Hobson <pmhobson@…287…> wrote:

On Mon, Sep 24, 2012 at 12:21 AM, Paul Tremblay > > >> >> <paulhtremblay@…287…> > > >> >> wrote:

Here is my example of a Pareto chart.

For an explanation of a Pareto chart:

http://en.wikipedia.org/wiki/Pareto_chart

Could I get this chart added to the matplolib gallery?

Thanks

Paul

On 9/24/12 4:40 PM, Benjamin Root wrote:

Your code looks overly complicated. You shouldn’t have to be doing the

connection to the ylim_changed event, I don’t think. I think your main

problem is that you are calling ax1.plot instead of ax2.plot.

I am not against adding more examples to the gallery, but this would

have to

be cleaned up before it gets included.

Ben Root

On Mon, Sep 24, 2012 at 5:50 PM, Paul Tremblay <paulhtremblay@…287…> > > >> > wrote:

I took my example from the matplotlib pages itself:

http://matplotlib.org/examples/api/fahrenheit_celcius_scales.html

If you know a better way, please show me.

P.

Paul,

That example is an overly complicated template for making a pareto

chart.

Here’s how I’d do it:

data

defects = [0, 32, 22, 15, 5, 2]

labels = [’’, ‘vertical’, ‘horizontal’, ‘behind’, ‘left area’, ‘other’]

axes

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()

plotting

ax1.bar(np.arange(len(defects))-0.4, defects, zorder=0, alpha=0.5)

ax2.plot(np.cumsum(defects), linestyle=’-’, color=‘k’, linewidth=2,

zorder=5)

formatting

ax1.set_xticks(np.arange(len(defects)))

ax1.set_xticklabels(labels)

ax1.set_ylabel(‘Defects’)

ax2.set_ylabel(‘Percentage’)

plt.show()


Live Security Virtual Conference

Exclusive live event will cover all the ways today’s security and

threat landscape has changed and how IT managers can respond.

Discussions

will include endpoint security, mobile security and the latest in

malware

threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi,

Just a detail :

percent = (np.divide(the_cumsum, the_sum)) * 100
This lines doesn’t work on my computer (numpy 1.6.2)

Indeed, there is a casting issue :

In [2]: percent

Out[2]: array([  0,   0,   0,   0, 100])

However, using the regular "/" operator instead of np.divide gives

the proper result:

In [8]: the_cumsum/the_sum*100

Out[8]: array([  42.10526316,   71.05263158,   90.78947368,  

97.36842105, 100. ])

Best,

Pierre
···

Le 26/09/2012 04:29, Paul Tremblay a écrit :

Actually, if you are using the latest numpy (the 1.7 beta), that will also not work unless you are using py3k or did “from future import division”. Well, actually, using np.divide will always result in integer division (this may or may not be a bug).

The correct thing to do until we move on to py3k with its “true division” is to make sure we cast one of the operands as floats: the_sum.astype(‘f’). Plus, using ‘/’ is more concise and readable.

Cheers!
Ben Root

···

On Wed, Sep 26, 2012 at 4:31 AM, Pierre Haessig <pierre.haessig@…1709…> wrote:

Hi,

Just a detail :



Le 26/09/2012 04:29, Paul Tremblay a écrit :

percent = (np.divide(the_cumsum, the_sum)) * 100

This lines doesn’t work on my computer (numpy 1.6.2)

Indeed, there is a casting issue :


In [2]: percent

Out[2]: array([  0,   0,   0,   0, 100])




However, using the regular "/" operator instead of np.divide gives

the proper result:

In [8]: the_cumsum/the_sum*100

Out[8]: array([  42.10526316,   71.05263158,   90.78947368    ,  

97.36842105, 100. ])

Best,

Pierre

Good point, I forgot I had set "from __future__ import division" some
months ago in my IPython startup settings.

So indeed, explicit casting to float is the safest approach.

Best,
Pierre

···

Le 26/09/2012 15:25, Benjamin Root a écrit :

Actually, if you are using the latest numpy (the 1.7 beta), that will
also not work unless you are using py3k or did "from __future__ import
division". Well, actually, using np.divide will always result in
integer division (this may or may not be a bug).

Thanks. I know when doing 8/9 in python 2.x you get 0. With python 3 you get a decimal (Hooray, Python 3!).

I ran the script I submitted with python 3. Do I need to change the defects and totals from integers to floats to make my chart work universally?

P.

···

On Wed, Sep 26, 2012 at 4:31 AM, Pierre Haessig <pierre.haessig@…1709…> wrote:

Hi,

Just a detail :



Le 26/09/2012 04:29, Paul Tremblay a écrit :

percent = (np.divide(the_cumsum, the_sum)) * 100

This lines doesn’t work on my computer (numpy 1.6.2)

Indeed, there is a casting issue :


In [2]: percent

Out[2]: array([  0,   0,   0,   0, 100])




However, using the regular "/" operator instead of np.divide gives

the proper result:

In [8]: the_cumsum/the_sum*100

Out[8]: array([  42.10526316,   71.05263158,   90.78947368    ,  

97.36842105, 100. ])

Best,

Pierre

Live Security Virtual Conference

Exclusive live event will cover all the ways today’s security and

threat landscape has changed and how IT managers can respond. Discussions

will include endpoint security, mobile security and the latest in malware

threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

That or just one of the two. Either way would work.

Ben Root

···

On Wed, Sep 26, 2012 at 12:10 PM, Paul Tremblay <paulhtremblay@…287…> wrote:

Thanks. I know when doing 8/9 in python 2.x you get 0. With python 3 you get a decimal (Hooray, Python 3!).

I ran the script I submitted with python 3. Do I need to change the defects and totals from integers to floats to make my chart work universally?

P.

Here is my latest version of the Pareto chart. If the developers
feel it is worthy, then they can include it in the gallery.
Otherwise, it will have to remain in these threads, useful when,
needing such a chart, someone searches the web and ends up here.
import matplotlib.pyplot as plt
import numpy as np

the data to plot

defects = [32.0, 22.0, 15.0, 5.0, 2.0]
labels = [‘vertical’, ‘horizontal’, ‘behind’, ‘left area’, ‘other’]
the_sum = sum(defects) # ie, 32 + 22 + 15 + 5 + 2
the_cumsum = np.cumsum(defects) # 32, 32 + 22, 32 + 22 + 15, 32 +
22 + 15 + 5, 32 + 22, + 15 + 5 + 2
percent = (np.divide(the_cumsum, the_sum)) * 100
ind = np.arange(len(defects)) # the x locations for the groups
width = .98 # with do of the bars, where a width of 1 indidcates no
space between bars
x = ind + .5 * width # find the middle of the bar
fig = plt.figure() # create a figure
ax1 = fig.add_subplot(111) # and a subplot
ax2 = ax1.twinx() # create a duplicate y axis
rects1 = ax1.bar(ind, defects, width=width) # draw the chart
line, = ax2.plot(x, percent) # draw the line
ax1.set_ylim(ymax=the_sum) # without these limits, graphs will not
work
ax2.set_ylim(0, 100)
ax1.set_xticks(x) # set ticks for middle of bars
ax1.set_xticklabels(labels) # create the labels for the bars
ax1.set_ylabel(‘Defects’) # create the left y axis label
ax2.set_ylabel(‘Percentage’) # create the right y axis label
plt.show()

···

On 9/26/12 12:31 PM, Benjamin Root
wrote:

    On Wed, Sep 26, 2012 at 12:10 PM, Paul

Tremblay <paulhtremblay@…287…>
wrote:

      Thanks. I know when doing 8/9 in python 2.x you get 0. With

python 3 you get a decimal (Hooray, Python 3!).

      I ran the script I submitted with python 3. Do I need to

change the defects and totals from integers to floats to make
my chart work universally?

          P.