Plotly Bar Chart Axes - charts

Im trying to create a simple bar chart and deploy it within a dropdown menu. My problem is with the axes. I do not know why when I use px.bar the deploy just take one variable (x or y) and make the plot, but when I use px.scatter it works, it takes both variables.
from dash import Dash, dcc, html, Input, Output
import plotly.express as px
import pandas as pd
import numpy as np
app = Dash(__name__)
pf = fr.iloc[2:10,18:25]
pf.columns = [prueba1]
pf.index = ['CH + CHL','CH','CHL(UST)','CHL','CL + CHL','CL','CHL(UST)','CHL']
app.layout = html.Div(children=[
html.Div([dcc.Dropdown(pf.index,'CH', id='pandas-dropdown-2'),
html.Div(id='pandas-output-container')]),
dcc.Graph(id='example-graph')
])
Output('example-graph','figure'),
Input('pandas-dropdown-2', 'value'))
def update_graph(value_input):
pff = pf[pf.index == value_input]
fig = px.bar(pff, x = np.array(prueba1), y = list(pff.values))#,
return fig
__name__ == '__main__':
app.run_server(debug=True, use_reloader=False)
The result is this one:
enter image description here
Why the Y label is count instead of years?
But when I change from px.bar to px.scatter this happens:
enter image description here
It works!!! But I need the bar chart!

Related

Issue when adding new node in graph

Im having some issues while trying to add a new node to a graph (with OSMNX)
I need to calculate some distances on some areas that dont have nodes near.
Here is my code:
import networkx as nx
import osmnx as ox
from IPython.display import IFrame
import geopandas as gpd
from shapely.geometry import Point
my_dict = {
'001': {
'y': -31.640224888841907,
'x': -60.672566478771884,
'street_count': 1
}
}
tmp_list = []
for item_key, item_value in my_dict.items() :
tmp_list.append({
'geometry' : Point(item_value['x'], item_value['y']),
'osmid': item_key,
'y' : item_value['y'],
'x' : item_value['x'],
'street_count': item_value ['street_count']
})
my_nodes = gpd.GeoDataFrame(tmp_list)
G = ox.graph_from_place("Santa Fe, Santa Fe, Argentina", network_type="drive", buffer_dist=5000)
nodes= ox.graph_to_gdfs(G, nodes=True, edges=False)
edges= ox.graph_to_gdfs(G, edges=True, nodes=False)
nodes = nodes.append(my_nodes, ignore_index = True)
G2 = ox.graph_from_gdfs(nodes, edges)
m1 = ox.plot_graph_folium(G2, popup_attribute="name", weight=2, color="#8b0000")
dest = (-60.70916, -31.64553)
ori= (-60.66756, - 31.63719)
iniciocercano = ox.nearest_nodes(G2, ori[0], ori[1], return_dist=True)
finalcercano = ox.nearest_nodes(G2, dest[0], dest[1], return_dist=True)
pathDistance = nx.shortest_path_length(G2, iniciocercano[0], finalcercano[0], weight="length")
route = nx.shortest_path(G2, iniciocercano[0], finalcercano[0])
And the error that Im getting is: Input contains NaN.
I also notice that the original graph (G) has: 9423 nodes and 25013 edges. And the new graph (G2) has: 18847 nodes and 25013 which is pretty strange. Somehow the nodes are getting duplicate.
Thank you for your time.
Your my_nodes GeoDataFrame is not indexed by osmid like it needs to be, and like your nodes GeoDataFrame is.

Updating a Dash Callback using RadioItems

I am fairly new to python coding so I apologize in advance for my ignorance. I am trying to create a Dash App that drops outliers using standard deviation. The user selects a standard deviation using RadioItem inputs.
My question is what amendments do I need to make to my code so that the RadioItem value updates max_deviations using a callback?
Import packages, clean the data and define a query
import dash
import plotly.express as px
from dash import Dash, dcc, html, Input, Output, State
import pandas as pd
import numpy as np
app = dash.Dash(__name__)
server = app.server
df=pd.read_csv(r'C:\SVS_GIS\POWER BI\CSV_DATA\QSAS2021.csv', encoding='unicode_escape')
#SET DATE OF VALUATION
df['TIME'] = ((pd.to_datetime(df['Sale Date'], dayfirst=True)
.rsub(pd.to_datetime('01/10/2021', dayfirst=True))
.dt.days
)*-1)
df=df[df['TIME'] >= -365]
df = df.query("(SMA >=1 and SMA <= 3) and (LGA==60)")
prepare dataframe for dropping outliers
data = pd.DataFrame(data=df)
x = df.TIME
y = df.CHANGE
mean = np.mean(y)
standard_deviation = np.std(y)
distance_from_mean = abs(y - mean)
app layout
app.layout = html.Div([
html.Label("Standard Deviation Picker:", style={'fontSize':25, 'textAlign':'center'}),
html.Br(),
html.Label("1.0 = 68%, 2.0 = 95%, 3.0 = 99.7%", style={'fontSize':15,
'textAlign':'center'}),
html.Div(id="radio_items"),
dcc.RadioItems(
options=[{'label': i, 'value': i} for i in [1.0, 2.0, 3.0]],
value=2.0
),
html.Div([
dcc.Graph(id="the_graph")]
)])
callback
#app.callback(
Output("the_graph", "figure"),
Input("radio_items", 'value')
)
def update_graph(max_deviations):
not_outlier = distance_from_mean < max_deviations * standard_deviation
no_outliers = y[not_outlier]
trim_outliers = pd.DataFrame(data=no_outliers)
dff = pd.merge(trim_outliers, df, left_index=True, right_index=True)
return (dff)
fig = px.scatter(dff, x='TIME', y='CHANGE_y',
color ='SMA',
trendline='ols',
size='PV',
height=500,
width=800,
hover_name='SMA',
)
return dcc.Graph(id='the_graph', figure=fig)
if __name__ == '__main__':
app.run_server(debug=False)
Your dcc.RadioItems doesn't have an id prop. Add that, and make sure it matches the ID given in the callback, and you should be good.

tesseract not able to detect korean language properly

I am learning how to detect Korean text, for sample I am using Korean text present in back of package, but pytesseract.image_to_string(img_pl,lang='kor') is not able to segregate words separately when I query with level set to word
Here is my code
import numpy as np
import pandas as pd
import cv2
import PIL
import pytesseract
import html
import io
import os
img_cv2 = cv2.imread('/Users/aniketdeshmukh/Desktop/Korean Text Images/0.png')
img_pl = PIL.Image.open('/Users/aniketdeshmukh/Desktop/Korean Text Images/0.png')
text_pl = pytesseract.image_to_string(img_cv2,lang='kor')
text_pl = pytesseract.image_to_string(img_pl,lang='kor')
#print(text_pl)
data = pytesseract.image_to_data(img_pl,lang='kor')
dataList = list(map(lambda x: x.split('\t'),data.split('\n')))
df = pd.DataFrame(dataList[1:],columns=dataList[0])
#df
df.info
df.dropna(inplace=True)
col_int = ['level','page_num','block_num','par_num','line_num','word_num','left','top','width','height','conf']
df[col_int] = df[col_int].astype(int)
img_cv2 = cv2.imread('/Users/aniketdeshmukh/Desktop/Korean Text Images/0.png')
image = img_cv2.copy()
level = 'word'
for l,x,y,w,h,c in df[['level','left','top','width','height','conf']].values:
if level == 'page':
if l == 1:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,0),2)
else:
continue
elif level == 'block':
if l == 2:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
else:
continue
elif level == 'para':
if l == 3:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
else:
continue
elif level == 'line':
if l == 4:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)
else:
continue
elif level == 'word':
if l == 5:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
else:
continue
cv2.imshow("bounding box",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
output I get is following even when level is set to 'word'

Changing the size and symbol of scatter chart plot points in ScalaFX

I want to make a linear regression program which visualizes the data to user. I'm using EJML for calculations and ScalaFX for front end. Everything is going fine but when I plot the data using Scatter Chart, the line drawn from the data is set to be rectangles which cover up the original data points. I would like to know how I can change the size, shape and transparency etc. of the plotted points.
Almost all of guides around JavaFX say that I should modify the CSS file (which doesn't automatically exist) in order to style my chart. I don't know how to do that in ScalaFX or even that is it possible to do that way. My result of searching every possible tutorial has been fruitless.
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.chart.ScatterChart
import scalafx.collections.ObservableBuffer
import scalafx.scene.chart.NumberAxis
import scalafx.scene.chart.XYChart
import scalafx.scene.shape.Line
import org.ejml.simple.SimpleMatrix
import scala.math.pow
import scala.collection.mutable.Buffer
object Plotting extends JFXApp {
/*
* Below are some arbitrary x and y values for a regression line
*/
val xValues = Array(Array(1.0, 1.0, 1.0, 1.0, 1.0, 1.0), Array(14.0, 19.0, 22.0, 26.0, 31.0, 43.0))
val yValues = Array(Array(51.0, 57.0, 66.0, 71.0, 72.0, 84.0))
val temp = yValues.flatten
val wrapper = xValues(1).zip(temp)
/*
* In the lines before stage what happens is that matrices for the x and y values are created, coefficients
* for the regression line are calculated with matrix operations and (x, y) points are calculated for the
* regression line.
*/
val X = new SimpleMatrix(xValues).transpose
val Y = new SimpleMatrix(yValues).transpose
val secondX = new SimpleMatrix(xValues(0).size, 2)
for (i <- 0 until xValues(0).size) {
secondX.set(i, 0, xValues(0)(i))
secondX.set(i, 1, xValues(1)(i))
}
val invertedSecondX = secondX.pseudoInverse()
val B = invertedSecondX.mult(Y)
val graphPoints = Buffer[(Double, Double)]()
for (i <- 0 to xValues(1).max.toInt) {
graphPoints.append((i.toDouble, B.get(0, 0) + i * B.get(1, 0)))
}
stage = new JFXApp.PrimaryStage {
title = "Demo"
scene = new Scene(400, 400) {
val xAxis = NumberAxis()
val yAxis = NumberAxis()
val pData = XYChart.Series[Number, Number](
"Data",
ObservableBuffer(wrapper.map(z => XYChart.Data[Number, Number](z._1, z._2)): _*))
val graph = XYChart.Series[Number, Number](
"RegressionLine",
ObservableBuffer(graphPoints.map(z => XYChart.Data[Number, Number](z._1, z._2)): _*))
val plot = new ScatterChart(xAxis, yAxis, ObservableBuffer(graph, pData))
root = plot
}
}
}
This certainly isn't as well documented as it might be... :-(
Stylesheets are typically placed in your project's resource directory. If you're using SBT (recommended), this would be src/main/resources.
In this example, I've added a stylesheet called MyCharts.css to this directory with the following contents:
/* Blue semi-transparent 4-pointed star, using SVG path. */
.default-color0.chart-symbol {
-fx-background-color: blue;
-fx-scale-shape: true;
-fx-shape: "M 0.0 10.0 L 3.0 3.0 L 10.0 0.0 L 3.0 -3.0 L 0.0 -10.0 L -3.0 -3.0 L -10.0 0.0 L -3.0 3.0 Z ";
-fx-opacity: 0.5;
}
/* Default shape is a rectangle. Here, we round it to become a red circle with a white
* center. Change the radius to control the size.
*/
.default-color1.chart-symbol {
-fx-background-color: red, white;
-fx-background-insets: 0, 2;
-fx-background-radius: 3px;
-fx-padding: 3px;
}
color0 will be used for the first data series (the regression line), color1 for the second (your scatter data). All other series use the default, JavaFX style.
(For more information on using scalable vector graphics (SVG) paths to define custom shapes, refer to the relevant section of the SVG specification.)
To have this stylesheet used by ScalaFX (JavaFX), you have a choice of options. To have them apply globally, add it to the main scene (which is what I've done below). Alternatively, if each chart needs a different style, you can add different stylesheets to specific charts. (BTW, I also added the standard includes import as this prevents many JavaFX-ScalaFX element conversion issues; otherwise, I've made no changes to your sources.)
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.chart.ScatterChart
import scalafx.collections.ObservableBuffer
import scalafx.scene.chart.NumberAxis
import scalafx.scene.chart.XYChart
import scalafx.scene.shape.Line
import org.ejml.simple.SimpleMatrix
import scala.math.pow
import scala.collection.mutable.Buffer
object Plotting extends JFXApp {
/*
* Below are some arbitrary x and y values for a regression line
*/
val xValues = Array(Array(1.0, 1.0, 1.0, 1.0, 1.0, 1.0), Array(14.0, 19.0, 22.0, 26.0, 31.0, 43.0))
val yValues = Array(Array(51.0, 57.0, 66.0, 71.0, 72.0, 84.0))
val temp = yValues.flatten
val wrapper = xValues(1).zip(temp)
/*
* In the lines before stage what happens is that matrices for the x and y values are created, coefficients
* for the regression line are calculated with matrix operations and (x, y) points are calculated for the
* regression line.
*/
val X = new SimpleMatrix(xValues).transpose
val Y = new SimpleMatrix(yValues).transpose
val secondX = new SimpleMatrix(xValues(0).size, 2)
for (i <- 0 until xValues(0).size) {
secondX.set(i, 0, xValues(0)(i))
secondX.set(i, 1, xValues(1)(i))
}
val invertedSecondX = secondX.pseudoInverse()
val B = invertedSecondX.mult(Y)
val graphPoints = Buffer[(Double, Double)]()
for (i <- 0 to xValues(1).max.toInt) {
graphPoints.append((i.toDouble, B.get(0, 0) + i * B.get(1, 0)))
}
stage = new JFXApp.PrimaryStage {
title = "Demo"
scene = new Scene(400, 400) {
// Add our stylesheet.
stylesheets.add("MyCharts.css")
val xAxis = NumberAxis()
val yAxis = NumberAxis()
val pData = XYChart.Series[Number, Number](
"Data",
ObservableBuffer(wrapper.map(z => XYChart.Data[Number, Number](z._1, z._2)): _*))
val graph = XYChart.Series[Number, Number](
"RegressionLine",
ObservableBuffer(graphPoints.map(z => XYChart.Data[Number, Number](z._1, z._2)): _*))
val plot = new ScatterChart(xAxis, yAxis, ObservableBuffer(graph, pData))
root = plot
}
}
}
For further information in the CSS formatting options available (changing shapes, colors, transparency, etc.) refer to the JavaFX CSS Reference Guide.
The result looks like this:
I almost don't dare to add somethig to Mike Allens solution (wich is very good, as always), but this did not work out for me because I could not get my scala to find and/or process the .css file.
I would have done it this way if possible, but I just could not get it to work.
Here is what I came up with:
Suppose I have some data to display:
val xyExampleData: ObservableBuffer[(Double, Double)] = ObservableBuffer(Seq(
1 -> 1,
2 -> 4,
3 -> 9))
Then I convert this to a Series for the LineChart:
val DataPoints = ObservableBuffer(xyExampleData map { case (x, y) => XYChart.Data[Number, Number](x, y) })
val PointsToDisplay = XYChart.Series[Number, Number]("Points", DataPoints)
now I put this again into a Buffer, maybe with some other data from different series.
val lineChartBuffer = ObservableBuffer(PointsToDisplay, ...)
and finally I create my lineChart, wich I call (with lack of creativity) lineChart:
val lineChart = new LineChart(xAxis, yAxis, lineChartBuffer) {...}
The lines between data points can be recoloured now easily with:
lineChart.lookup(".default-color0.chart-series-line").setStyle("-fx-stroke: blue;")
This will change the Line-colour of the FIRST Dataset in the LineChartBuffer.
If you want to change the Line-Properties for the second you call
lineChart.lookup(".default-color1.chart-series-line")...
There is also "-fx-stroke-width: 3px;" to set the with of the line.
"-fx-opacity: 0.1;"
"-fx-stroke-dash-array: 10;"
-fx-fill: blue;"
are also usefull, but dont call the above line repeatedly, because the second call will override the first.
Instead concatenate all the strings into one:
lineChart.lookup(".default-color0.chart-series-line").setStyle("-fx-stroke: blue;-fx-opacity: 0.1;-fx-stroke-dash-array: 10;-fx-fill: blue;")
Now for the formatting of the Symbols at each data-Point:
unfortunately there seems to be no other way than to format each Symbol seperately:
lineChart.lookupAll(".default-color0.chart-line-symbol").asScala foreach { node => node.setStyle("-fx-background-color: blue, white;") }
for this to run you need import scala.collection.JavaConverters._
for the conversion from a java set to a scala set.
One can also make all data-poins from only one data-set invisible, for example:
lineChart.lookupAll(".default-color1.chart-line-symbol").asScala foreach { node => node.setVisible(false) }
To say this is a nice solution would be exaggerated.
And it has the big disadvantage, that you have to recolour or reformat every Symbol after adding a new Datapoint to one of the series in LineChartBuffer. If you don't, the new Symbols will have standard colours and settings.
The Lines stay, ones they are recoloured, I can't say why.
But the good side of it, one can always reformat curves in a Line Chart afterwards like this!

Suppress output from an interact() widget

I am using a widget to parametrise the generation of some data. I would like to capture the data, without outputting it. Is this possible? Adding ; after interact() doesn't work. Example:
import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed
def generate_data(n, p, s):
return np.random.negative_binomial(n, p, s)
w_n = widgets.IntSlider(min=1, max=10000, step=1)
w_p = widgets.FloatSlider(min=0.01, max=1, step = 0.01)
w_s = widgets.IntSlider(min=500,max=10000,step=50)
data = interact(generate_data, n = w_n, p = w_p, s = w_s);
Instead of returning a value in generate_data(), you could modify a global variable. It avoids printing the output, and you can use val later in your code.
import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed
import numpy as np
val = None
def generate_data(n, p, s):
global val
val = np.random.negative_binomial(n, p, s)
w_n = widgets.IntSlider(min=1, max=10000, step=1)
w_p = widgets.FloatSlider(min=0.01, max=1, step = 0.01)
w_s = widgets.IntSlider(min=500,max=10000,step=50)
interact(generate_data, n = w_n, p = w_p, s = w_s)
I have submitted a pull request to ipywidgets that addresses this problem:
https://github.com/ipython/ipywidgets/pull/712
The changes are actually very simple. With this version, you would simply pass an extra kwarg to interact:
data = interact(generate_data, n = w_n, p = w_p, s = w_s,
__output_result=False)