Bar and Line charts are not synced when in the same chart area - vb.net

I have problem with a chart in vb.net. The problem is that line and bar are not synced in the chart area. I've attached a picture to make it clear what I mean
Here is the code where I populate the chart. I´m getting the data from a database.
Dim theDate As Date
For i As Integer = Count - 1 To 0 Step -1
'Chart1.Series("serRxTime").Points.AddY(dv(i)(0) / 60)
theDate = dv(i)(1)
Chart1.Series("serTime").Points.AddXY(theDate.ToString("dd-MMM HH:MM", enUS), dv(i)(0) / 60)
Chart1.Series("serAdd").Points.AddY(dv(i)(2))
Next

Line and column series have the same XValues that's why their centres are aligned. You would need to generate different XValues for the two series. XValues that are offset by a small margin. Something like this:
Chart1.Series("serTime").XValues = {0.8, 1.8, 2.8, 3.8,,...,count - 0.2}
Chart1.Series("serAdd").XValues = {1, 2, 3, 4,..., count}
I used 0.2 difference, but this will be different in your case (especially since it seems you have date axis set?). This would push the line series to the left.
I created an example for you. On the first picture you can see the data for the columns. Their x values are 1,2,3,4,...,12 and their y values are marked with blue.
And this is the values for the XY chart. As you can see I moved the x values by 0.2 to the left.

Related

Matplotlib graph axis changing [duplicate]

This question already has answers here:
matplotlib generating strange y-axis on certain data sets?
(1 answer)
How to prevent numbers being changed to exponential form in a plot
(6 answers)
Matplotlib yaxis range display using absolute values rather than offset values?
(3 answers)
How can I change plt.plot x axis from 0 to real value?
(1 answer)
matplotlib axis label format
(2 answers)
Closed 1 year ago.
I am trying to make graph using pandas and matplotlib. My data in x and y axis is as follows
Data in X ( It is a python list)
[99.50133729728567,
99.32177485513289,
99.29662080782298,
99.1922871744391,
99.14070177652418,
99.29499371273404,
99.54826208274174,
99.85501632675789,
99.98246755801047,
100.28450605132474,
100.69047659277543,
100.82812684703163,
100.67670430280266,
100.39417022400526,
100.22792692751301,
99.9158443902431,
99.55075159185297,
99.330916993794,
99.37737347787801,
99.62973414137957,
100.01866583740237,
100.42758798072411,
100.56199275721731,
100.48335938102836,
100.313649654463,
100.16496347602485,
99.82351418830224,
99.46621756385987,
99.33208175544729,
99.2794271217398,
99.42925472046936,
99.52825492237166,
99.7573717856461,
99.96122191358516,
100.1013977818942,
100.26595437442364]
Data in Y ( It is a python list)
[100.00020398087408,
100.00043468828244,
100.00050028851648,
100.00030297162272,
99.9997920462273,
99.99883929230562,
99.99906856654064,
99.99950793983656,
99.9986149630411,
99.9982943411356,
99.99939115721274,
100.00021418167366,
100.0008698442229,
100.00061513516697,
100.00129114064856,
100.00151549197089,
100.00027715439002,
100.00023658939669,
99.99899872492574,
99.99747708221712,
99.99758605940052,
99.9990886473757,
100.00046810814177,
100.00045794761733,
100.00090169642483,
100.00184415283576,
100.0021535400512,
100.00079677558163,
100.0005244158289,
99.99967151341512,
100.00009168515908,
99.99958972419704,
99.99920652259239,
99.99939034471534,
99.99908335764445,
100.00043148740218]
I am using following code to make the graph
plt.plot(ss['x'].iloc[0:n], ss['y'].iloc[0:n], linestyle="-", marker='>', label=sector)
plt.legend()
plt.show()
The graph shows like this:
I want to achieve the following:
The x axis seems fine, with scale around 100. But y axis changes the scale on its own as can be seen in the image. While actual y data is of the order of magnitude around 100, the graph has order of magnitude .0011. I am not sure why this change on its own and how to correct it and get around 100.
To find out the range of your Y values you can do this:
print(min(Y), max(Y))
This gives:
99.99747708221712 100.0021535400512
The distance from min(Y) to max(Y) is only 0.0046. That is why matplotlib has plotted the Y axis as a range (-0.002 to +0.002) + an offset of '1e2', which is shown at the top left. '1e2' means "1 x 10 to the power of 2", i.e. 100. Therefore the y-axis range is correct: 99.998 to 100.002.
My guess is, that it shows the labels as -0.002, -0.001, 0.000, ... etc. +1e2 because they are easier to read than 99.998, 99.999, 100.000, ... etc.
UPDATE:
I looked in the documentation and there is a command for setting the tick label behaviour. Use this setting to turn off the behaviour above:
plt.ticklabel_format(axis='y', useOffset=False)
Then the labels are 'normal':

How to set in range in hexbin plots pandas?

I want to set range of y-axis in the following code.My values are [19,20,21,22,23,24.....,40].I want the labels to be starting from of difference 10,starting from 10 upto 40.How should I set that.
I was trying to use x-lim but it is not working.
plt.hexbin(p_df['Age'], p_df['Salary'], gridsize = 50,bins ='log',cmap ='BuGn',xlim =(19, 40))
To set the range of values to include in the binning, you must use the 'extent' keyword. So to have a hexbin plot with a range between your desired x and y values, you can use,
extent=[-x,x,10,40]
where x is your included x coordinates.
So your new code would be
plt.hexbin(p_df['Age'], p_df['Salary'], gridsize = 50,bins ='log', extent=[-x,x,10,40],cmap ='BuGn')

Graph to show departure and arrival times between stations

I have the start and end times of trips made by a bus, with the times in an Excel sheet. I want to make the graph as below :
I tried with Matlab nodes and graphs but did not got the exact figure, below is the Matlab code which I tried as an example:
A = [1 4]
B = [2 3]
weights = [5 5];
G = digraph(A,B,weights,4)
plot(G)
And the figure it generates:
I have got many more than 4 points in the Excel sheet, and I want them to all be displayed as in the first image.
Overview
You don't need any sort of complicated graph package for this, just use normal line plots! Here are methods in Excel and Matlab.
Excel
Give each bus stop a number, and list the bus stop number by the time it arrives/leaves there. I'll use stops number 0 and 1 for this example.
0 04:41
1 05:35
1 05:40
0 06:34
0 06:51
1 07:45
1 15:21
0 16:15
Then simply highlight the data and insert a "scatter with straight lines"
The rest is formatting. You can format the y-axis and tick "values in reverse order" to get the time increasing as in your desired plot. You can change the x-axis tick marks to just show integer stop numbers, get rid of the legend etc.
Final output:
Matlab
Here is the Matlab documentation for converting Excel formatted dates into Matlab datetime arrays: Convert Excel Date Number to Datetime.
Once you have the datetime objects, you can do this easily with the standard plot function.
% Set times up as a datetime array, could do this any number of ways
times = datetime(strcat({'1/1/2000 '}, {'04:41', '05:35', '05:40', '06:34', '06:51', '07:45', '15:21', '16:15'}, ':00'), 'format', 'dd/MM/yyyy HH:mm:ss');
% Set up the location of the bus at each of the above times
station = [0,1,1,0,0,1,1,0];
% Plot
plot(station, times) % Create plot
set(gca, 'xtick', [0,1]) % Limit to just ticks at the 2 stops
set(gca, 'ydir', 'reverse') % Reverse y axis to have earlier at top
set(gca,'XTickLabel',{'R', 'L'}) % Name the stops
Output:

Scale chart Y axis labels

I have a vb.net windows form chart control on a form that displays a line chart of data that is made available in milli-gravities.
e.g. 5 gravities is given as 5000 milli-gravities, meaning that the Y-axis label is 5000
Is it possible to plot the same data but, have the Y-axis use labels in gravities, instead of milli-gravities? That is, for a Y data point of 5000, the Y axis label should be 5, or in other words have label value = auto label value / 1000
The data is currently bound to the chart using the following syntax
Chart1.Series.Add("Z Axis") 'Add multiple series
Chart1.Series.Add("Z RMS")
For Each Series In Chart1.Series 'Set each series to line type
Series.ChartType = SeriesChartType.Line
Next
Chart1.ChartAreas(0).AxisY.Maximum = 100000 'Start the Y axis limits at 100 gravities
Chart1.ChartAreas(0).AxisY.Minimum = 100000 'The max & min are changeable by user input
Chart1.ChartAreas(0).RecalculateAxesScale()
Chart1.Series("Z Axis").Points.DataBindY(record.Samples, "Z") 'Add the Y data points
Chart1.Series("Z RMS").Points.DataBindY(record.Samples, "RmsZ")
I thought maybe setting the Chart1.ChartAreas(0).AxisY.LabelStyle.Format might help, but I'm not sure how to use a format string to shorten an integer?

VB2010 Setting logarithmic scale intervals

I'm developing a financial application in which I need to display data in a chart with a logarithmic scale on the Y axis. Everything works fine except for the intervals. With the following:
chart.ChartAreas(0).AxisY.IsLogarithmic = True
chart.ChartAreas(0).AxisY.LogarithmBase = 10
chart.ChartAreas(0).AxisY.Interval = 1
chart.ChartAreas(0).AxisY.Minimum = CalcMinYVal(minYVal)
I get the CalcMinYVal multiplied by 10^0,10^1,10^2,10^3 and so on for the Y-axis values.
I would like to have the Y axis values increased by 1. How can I have the interval be REALLY 1?
You can enable the MinorGrid property
chart.ChartAreas(0).AxisY.MinorGrid = True
to show the horizontal lines in between the powers of 10 like shown below.
But there is a limitation in showing the value for each subdivision. They can only appear in fixed intervals by using the Interval property of the LabelStyle.
For example to show 10 subdivisions, you can set:
Chart.ChartAreas(0).AxisY.LabelStyle.Interval = 0.1
The number of the horizontal lines of the MinorGrid can be controlled by using its Interval propery:
Chart.ChartAreas(0).AxisY.MinorGrid.Interval = 1
and the values of the labels can be rounded by using the format property:
Chart.ChartAreas(0).AxisY.LabelStyle.Format = "{0.0}"