You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[!INCLUDE[SQL Server SQL DB SQL MI](../../includes/applies-to-version/sql-asdb-asdbmi.md)]
16
17
17
18
This article describes how to plot data using the Python package [pandas'.hist()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.hist.html). A SQL database is the source used to visualize the histogram data intervals that have consecutive, non-overlapping values.
*[SQL Server Management Studio](../../ssms/download-sql-server-management-studio-ssms.md) for restoring the sample database to Azure SQL Managed Instance.
33
+
-[SQL Server Management Studio](../../ssms/download-sql-server-management-studio-ssms.md) for restoring the sample database to Azure SQL Managed Instance.
33
34
::: moniker-end
34
35
35
-
* Azure Data Studio. To install, see [Azure Data Studio](../../azure-data-studio/what-is-azure-data-studio.md).
36
+
- Azure Data Studio. To install, see [Azure Data Studio](../../azure-data-studio/what-is-azure-data-studio.md).
36
37
37
-
*[Restore sample DW database](../../samples/adventureworks-install-configure.md) to get sample data used in this article.
38
+
-[Restore sample DW database](../../samples/adventureworks-install-configure.md) to get sample data used in this article.
38
39
39
-
## Verify restored Database
40
+
## Verify restored database
40
41
41
42
You can verify that the restored database exists by querying the **Person.CountryRegion** table:
43
+
42
44
```sql
43
45
USE AdventureWorksDW;
44
46
SELECT*FROMPerson.CountryRegion;
45
47
```
46
-
48
+
47
49
## Install Python packages
48
50
49
51
[Download and Install Azure Data Studio](../../azure-data-studio/download-azure-data-studio.md).
50
52
51
53
Install the following Python packages:
52
-
* pyodbc
53
-
* pandas
54
+
-`pyodbc`
55
+
-`pandas`
56
+
-`sqlalchemy`
57
+
-`matplotlib`
54
58
55
-
To install these packages:
59
+
To install these packages:
56
60
57
-
1. In your Azure Data Studio notebook, select **Manage Packages**.
58
-
2. In the **Manage Packages** pane, select the **Add new** tab.
59
-
3. For each of the following packages, enter the package name, click**Search**, then click**Install**.
61
+
1. In your Azure Data Studio notebook, select **Manage Packages**.
62
+
1. In the **Manage Packages** pane, select the **Add new** tab.
63
+
1. For each of the following packages, enter the package name, select**Search**, then select**Install**.
60
64
61
65
## Plot histogram
62
66
63
-
The distributed data displayed in the histogram is based on a SQL query from AdventureWorksDW. The histogram visualizes data and the frequency of data values.
64
-
Edit the connection string variables: 'server', 'database', 'username', and 'password' to connect to SQL database.
67
+
The distributed data displayed in the histogram is based on a SQL query from `AdventureWorksDW`. The histogram visualizes data and the frequency of data values.
68
+
69
+
Edit the connection string variables: 'server', 'database', 'username', and 'password' to connect to SQL Server database.
65
70
66
71
To create a new notebook:
67
72
68
73
1. In Azure Data Studio, select **File**, select **New Notebook**.
69
-
2. In the notebook, select kernel **Python3**, select the **+code**.
70
-
3. Paste code in notebook, select **Run All**.
74
+
1. In the notebook, select kernel **Python3**, select the **+code**.
75
+
1. Paste code in notebook, select **Run All**.
71
76
72
77
```python
73
78
import pyodbc
74
-
import pandas as plt
79
+
import pandas as pd
80
+
import matplotlib
81
+
import sqlalchemy
82
+
83
+
from sqlalchemy import create_engine
84
+
85
+
matplotlib.use('TkAgg', force=True)
86
+
from matplotlib import pyplot as plt
87
+
75
88
# Some other example server values are
76
89
# server = 'localhost\sqlexpress' # for a named instance
77
90
# server = 'myserver,port' # to specify an alternate port
sql ="SELECT DATEDIFF(year, c.BirthDate, GETDATE()) AS Age FROM [dbo].[FactInternetSales] s INNER JOIN dbo.DimCustomer c ON s.CustomerKey = c.CustomerKey"
85
-
df = plt.read_sql(sql, cnxn)
86
-
df.hist(bins=10)
100
+
101
+
df = pd.read_sql(sql, engine)
102
+
df.hist(bins=50)
103
+
104
+
plt.show()
87
105
```
88
106
89
-
The display shows the age distribution of customers in the FactInternetSales table.
107
+
The display shows the age distribution of customers in the `FactInternetSales` table.
90
108
91
-

109
+
:::image type="content" source="media/python-histogram.png" alt-text="Diagram showing the Pandas histogram distribution.":::
0 commit comments