Python matplot misses bars in bar graph - python-2.7

pyhon matplot misses bars in bar graph. I expect it to generate bar at [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000] but it shows only at [250, 350, 450, 550, 650, 750]
import matplotlib.pyplot as plt
nsamples = 1000 + 1
vpp = 25
timestamps = [t for t in range(0, nsamples, vpp)]
processings = [0 for t in range(0, nsamples, vpp)]
transmissions = [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 650, 700, 750, 800, 850, 900, 950, 1000]
for transmission in transmissions:
processings[transmission / vpp] = 1
plt.bar(timestamps, processings)
plt.show()

OK. I got it working somehow and i don't know details but when i reduced the sample size by using range(0, nsamples/vpp, 1) instead of range(0, nsamples, vpp) then graph was plotted as expected (even though number of elements to be plotted against on x axis was still same.) it seems plt.bar assumes unit step between axis values in argument list instead of just taking the list itself and plotting it.

Related

"Excess elements in scalar initializer" error in 2d array [duplicate]

This question already has answers here:
How do multidimensional arrays in C++ function? [duplicate]
(4 answers)
Closed 4 years ago.
I need to use 2 dimension array in my game so;
First I'M declaring it like this in my header file called "HelloWorldScene.h":
using namespace cocos2d;
class HelloWorld : public Layer
{
private:
...
int upgradeCosts[4][4];
...
}
Then I'm tryin to define it in my source file called "HelloWorldScene.cpp":
#include "HelloWorldScene.h"
...
bool HelloWorld::init()
{
....
upgradeCosts[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
...
}
But it gives me this error: "Excess elements in scalar initializer"
The entire code between brackets {{150, ...... , 1900 }} are underlined with red. And i dont know what to do, please help me.
This is absolutely wrong to initialize the whole array like this :
upgradeCosts[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
Because you just set the [4][4] element, not the whole array. This what the Excess elements in scalar initializer error refers to. An int is a scalar, it can only be initialised with 1 value, yet you attempt to initialise it with 16 values.
To correct this you would assume you could do this:
bool HelloWorld::init()
{
upgradeCosts = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
}
However this will not work because upgradeCosts has already been default initialised during the construction of HelloWorld and cannot be initialised again.
A naive solution to this would be like the following:
bool HelloWorld::init()
{
int data[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
for(int i = 0;i < 3;i++)
for(int j = 0;j < 3;j++)
upgradeCosts[i][j] = data[i][j];
...
}
However, this involves pointless copying. A better solution would be to move the array initialisation out of the init method altogether. You can do this by either just initialising the array in the header file:
// hpp file
class HelloWorld
{
private:
int upgradeCosts[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
public:
bool init();
};
Or you could initialise it in the constructor of HelloWorld:
// hpp file
class HelloWorld
{
private:
int upgradeCosts[4][4];
public:
HelloWorld();
bool init();
};
// cpp file
HelloWorld::HelloWorld()
: upgradeCosts{{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}}
{ }
Or if you know that your upgradeCosts should be the same across all instances of HelloWorld you can use a static member and initialise it like below :
// hpp file
class HelloWorld
{
private:
const static int upgradeCosts[4][4];
public:
bool init();
};
// cpp file
const int HelloWorld::upgradeCosts[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};

Why is the line function in library graphics.h not executing when iterated?

Here is my code. It is supposed to draw a bar chart every time it loops in main. It correctly displays the bar chart initially, but when looped, it seems like the line function does not work anymore....can anyone help me with this?
graphics library available here: http://www.mediafire.com/file/qtdy239vvr19qyi/CODE+BLOCKS+with+Graphic.h.rar
Note: I am using codeblocks 17.12
#include <graphics.h>
#include <conio.h>
#include <fstream>
#include <iostream>
using namespace std;
void graph(string f){
cleardevice();
int gd = DETECT, gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0, "BAR CHART");
setlinestyle(SOLID_LINE,0,2);
/* Draw X and Y Axis */
line(90,410,90,50); //Y axis
line(90,410,1100,410); //X axis
line(85,60,90,50); //Arrow code last 4 line
line(95,60,90,50);
line(1095,405,1100,410);
line(1095,415,1100,410);
line(84,360 ,95, 360);
line (84, 310 ,95,310);
line (84, 260, 95, 260);
line(84, 210, 95, 210);
line(84, 160, 95, 160);
line (84, 110, 95, 110);
outtextxy(34, 350, "50");
outtextxy(34, 300, "100");
outtextxy(34, 250, "150");
outtextxy(34, 200, "200");
outtextxy(34, 150, "250");
outtextxy(34, 100, "300");
outtextxy(150, 420, "Jan");
outtextxy(225, 420, "Feb");
outtextxy(300, 420, "Mar");
outtextxy(375, 420, "April");
outtextxy(450, 420, "May");
outtextxy(525, 420, "June");
outtextxy(600, 420, "Jul");
outtextxy(675, 420, "Aug");
outtextxy(750, 420, "Sept");
outtextxy(825, 420, "Oct");
outtextxy(900, 420, "Nov");
outtextxy(975, 420, "Dec");
outtextxy(65,60,"Y");
outtextxy(1080,415,"X");
outtextxy(70,415,"O");
/* Draw bars on screen */
//setfillstyle(XHATCH_FILL, RED);
bar(150,110,200,410);
bar(225,160,275,410);
bar(300,120,350,410);
bar(375,170,425,410);
bar(450,135,500,410);
getch();
closegraph();
//cleardevice();
}
int main(){
int choice = 1;
do{
graph();
cout<<"Key in zero to loop:..";
cin>>choice;
}while (choice == 0);
return 0;
}

Python can generated text be converted so it can be used in code

Crop was generated from a list:
count = 0
for Crop in param:
count = count +1
St=chr(ord('`') + count)
print St,"=im.crop(",Crop,")"
Resulted:
a =im.crop( (0, 0, 640, 0) )
b =im.crop( (0, 40, 640, 40) )
c =im.crop( (0, 80, 640, 80) )
d =im.crop( (0, 120, 640, 120) )
e =im.crop( (0, 160, 640, 160) )
f =im.crop( (0, 200, 640, 200) )
g =im.crop( (0, 240, 640, 240) )
h =im.crop( (0, 280, 640, 280) )
I want to use the results to actually crop an image in several pieces to re-assemble using the variables with Pillow.
You may be interested in the eval function - https://docs.python.org/3/library/functions.html#eval. Or alternatively, you could save the Python code that you have generated into a file, and import/run the file.
However, either of those options are generally discouraged, because input is not always trustworthy.
I offer you the following code, which parses your code string and runs it safely - that is, it does what you expect, crops images, without opening yourself up to running any other code that may be included.
from PIL import Image
im = Image.new('RGB', (1000, 1000))
input = """
a =im.crop( (0, 0, 640, 0) )
b =im.crop( (0, 40, 640, 40) )
c =im.crop( (0, 80, 640, 80) )
d =im.crop( (0, 120, 640, 120) )
e =im.crop( (0, 160, 640, 160) )
f =im.crop( (0, 200, 640, 200) )
g =im.crop( (0, 240, 640, 240) )
h =im.crop( (0, 280, 640, 280) )
"""
output = {}
for line in input.strip().split("\n"):
variable, operation = line.split('=')
if operation.startswith('im.crop('):
coords = operation.split('im.crop')[1].replace('(', '').replace(')', '').split(',')
result = im.crop([int(coord) for coord in coords])
output[variable.strip()] = result
print(output)

Drawing in-memory 8-bit greyscale image buffer to screen using wxWidgets

I would like to draw 8-bit grayscale image data to a wxPaintDC, but I have been having trouble. The data is made available to me in a uint8_t array. See the non-working code below (this is one of several attempts). I am able to draw using other wxPaintDC::Draw* functions, so I believe that everything else is set up fine.
uint8_t testBuffer[] =
{
250, 220, 222, 223, 210,
186, 22, 29, 89, 110,
250, 220, 222, 203, 210,
240, 120, 220, 123, 210,
230, 210, 252, 223, 210
};
wxBitmap cameraImage(5, 5, 8);
wxNativePixelData cameraImageData(cameraImage);
wxNativePixelData::Iterator p(cameraImageData);
for(unsigned y = 0; y < 5; ++y)
{
wxNativePixelData::Iterator rowStart = p;
for(unsigned x = 0; x < 5; ++x, ++p)
{
uint8_t value = testBuffer[5 * y + x];
p.Red() = value;
p.Green() = value;
p.Blue() = value;
}
p = rowStart;
p.OffsetY(cameraImageData, 1);
}
// _dialogDrawingCanvas is a wxPaintDC*
_dialogDrawingCanvas->DrawBitmap(cameraImage, 50, 50);
EDIT
I looked at wxImage based on the suggestion made by Thomas Matthews. Using wxImage works when I use testBuffer, but not when I use the actual buffer.
This works:
uint8_t testBuffer[] =
{
250, 220, 222, 223, 210,
186, 22, 29, 89, 110,
250, 220, 222, 203, 210,
240, 120, 220, 123, 210,
230, 210, 252, 223, 210
};
wxImage image(5, 5, testBuffer, true);
wxBitmap bmp(image);
_dialogDrawingCanvas->DrawBitmap(bmp, 50, 50);
However, the code below does not. I get a crash in the wxBitmap ctor.
// Returns instance of a class that provides access to camera image.
CameraImage camImage = grabCameraImage();
uint8_t* buf = camImage.getBuffer();
wxImage image(camImage.getWidth(), camImage.getHeight(), buf, true);
wxBitmap bmp(image); // Crashes here.
_dialogDrawingCanvas->DrawBitmap(bmp, 50, 50);
The crash I get is:
First-chance exception at 0x000007fdcf824b5b in [redacted].exe: 0xC0000005: Access violation reading location 0x000000000304d27a.
Unhandled exception at 0x000007fdcf824b5b in [redacted].exe: 0xC000041D: An unhandled exception was encountered during a user callback.
EDIT 2
I linked the wxWidgets DLL to its source code and traced into the call to the wxBitmap ctor. It looks like it's expecting at least 24BPP and starts reading at the end of the wxImage buffer and goes backwards so it's crashing as soon as it tries to access what it thinks is the last 3 bytes of the buffer. It looks like the depth argument to the wxBitmap constructor is ignored. I think maybe this wasn't an issue with testBuffer because it was a fixed size array.
This is basically answered in my edits, but the problem was caused by using 8 BPP data in the buffer. I was able to accomplish what I wanted to do as follows:
CameraImage camImage = grabCameraImage();
wxImage image(camImage.getWidth(), camImage.getHeight());
const uint8_t* cameraBuffer = camImage.getBuffer();
for(uint16_t y = 0; y < camImage.getHeight(); ++y)
{
for(uint16_t x = 0; x < camImage.getWidth(); ++x)
{
uint8_t intensity = cameraBuffer[y * camImage.getWidth() + x];
image.SetRGB(x, y, intensity, intensity, intensity);
}
}
wxBitmap bmp(image, 8);
_dialogDrawingCanvas->DrawBitmap(bmp, 50, 50);

Filling a Partially Rounded Rectangle with GDI+

I have a rounded rectangle that I make like so
dc.RoundRect(textBorder, CPoint(20, 20));
Later on I draw a line through it about 1/3 of the way down.
dc.LineTo(textBorder.right, textBorder.top + 15);
Now I would like to fill just the part above the line with a solid color. In other words I need to fill a partially rounded rectangle, because the top of the rectangle is rounded, but the bottom of it is truncated by the line. Is there an easy way to do this?
Have you tried using a combination of CreateRoundRectRegion and then FillRgn to fill the non-rectangular area?
This the example given in the docs for CreateRoundRectRegion:
CRgn rgnA, rgnB, rgnC;
VERIFY(rgnA.CreateRoundRectRgn( 50, 50, 150, 150, 30, 30 ));
VERIFY(rgnB.CreateRoundRectRgn( 200, 75, 250, 125, 50, 50 ));
VERIFY(rgnC.CreateRectRgn( 0, 0, 50, 50 ));
int nCombineResult = rgnC.CombineRgn( &rgnA, &rgnB, RGN_OR );
ASSERT( nCombineResult != ERROR && nCombineResult != NULLREGION );
CBrush brA, brB, brC;
VERIFY(brA.CreateSolidBrush( RGB(255, 0, 0) ));
VERIFY(pDC->FillRgn( &rgnA, &brA)); // rgnA Red Filled
VERIFY(brB.CreateSolidBrush( RGB(0, 255, 0) ));
VERIFY(pDC->FillRgn( &rgnB, &brB)); // rgnB Green Filled
VERIFY(brC.CreateSolidBrush( RGB(0, 0, 255) )); // rgnC Blue
VERIFY(pDC->FrameRgn( &rgnC, &brC, 2, 2 ));
In general, when you want to do something with non-rectangular areas you have to start looking into regions.