2

ここでは、パッケージをロードし、SQl クエリを記述して Pandas と統合し、最後に Bokeh を使用してプロットを表示しようとしていますが、bokeh には何も表示されません。

以下をデータセットと見なすことができますdf_new_2

             name       success_rate        failure_rate
             A          94.7                5.3
             B          94.3                5.7
             C          91                  9
             D          88                  13
             E          84                  16
             F          81                  19
             G          78                  22  
             H          74.6               25.4

コードはここから始まります

 import pandas.io.sql
 import pandas as pd
 import pyodbc
 from bokeh import mpl
 from bokeh.plotting import output_file,show 

 server = 'root'     #getting the server to work
 db = 'y'            #assigning database

 # Create the connection
 conn = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;PORT= 3306;DATABASE=y;UID=root;PWD=123456789;")
 cursor=conn.cursor()

 # query db- Here we are trying to count the number of success in a table and the name for which the success has been found by joining tables 
sql = """

   SELECT count(*) AS TOTAL,
   COUNT(CASE WHEN status=0 THEN 1 END) AS success,
   b.name
   FROM a
   JOIN b
   ON b.id=a.merchant
   GROUP BY merchant

   LIMIT 10
   """

  df = pandas.io.sql.read_sql(sql, conn)    #defining df as query result
  df.head()

  df_new=df.set_index('name')   #indexing as the name of a
  df_new['success_rate']=df_new['success']*100/df_new['TOTAL'] 
  df_new['failure_rate']=100-df_new['success_rate']     #assigning failure rate
  df_new2=pd.DataFrame(df_new,columns=['success_rate','failure_rate'])
  p=df_new2.plot(kind='barh',stacked=True)
  output_file("pandas_series.html", title="pandas_series.py example")   #assigning the name of output screen
  show(mpl.to_bokeh)   #showing the output of bokeh
4

1 に答える 1

1

got something a bit more useful for you now. Had to avoid mpl as I couldn't get that to work. One possible reason is that I don't think horizontal bar charts are available in bokeh.

import pandas as pd
from bokeh.charts import Bar
from bokeh.plotting import output_file, show 
from bokeh.charts.operations import blend
from bokeh.charts.attributes import cat, color

df_new2 = pd.DataFrame({'Success Rate' : [94.7,94.3,91,88,84,81,78,74.6], 'Failure Rate' : [5.3,5.7,9,12,16,19,22,25.4]})
df_new2['inds'] = ['A','B','C','D','E','F','G','H']

p = Bar(df_new2, 
        values=blend('Failure Rate','Success Rate', name='% Success/Failure', labels_name='stacked'),
        label=cat('inds'),
        stack=cat(columns='stacked', sort=False),
        color=color(columns='stacked', palette=['Red', 'Green'],
                              sort=False),
                  legend='top_right',
                  title="Success Rate vs. Failure Rate")
output_file("pandas_series.html", title="pandas_series.py example")   #assigning the name of output screen
show(p)   #showing the output of bokeh
于 2016-05-30T07:17:14.313 に答える