📅  最后修改于: 2023-12-03 15:04:09.141000             🧑  作者: Mango
Python is a versatile programming language that can be used to work with a variety of data. Tableau is a powerful data visualization tool that allows you to create interactive dashboards and data visualizations.
In this tutorial, we will explore how to use Python and Tableau together to create 2D data visualizations.
Before we begin, you'll need to have the following:
Before we can create a 2D data visualization, we need to prepare our data. In this example, we will be using a dataset that contains information about car sales.
import pandas as pd
# Load the data into a pandas dataframe
df = pd.read_csv('car_sales.csv')
# Print the first 5 rows of the dataframe
print(df.head())
Here is a sample of what the data looks like:
| Make | Model | Year | Sales | |------|-------|------|-------| | Honda | Civic | 2017 | 15000 | | Toyota | Corolla | 2017 | 20000 | | Ford | F150 | 2018 | 10000 | | Honda | Accord | 2018 | 12000 |
Next, we need to create a Tableau worksheet to visualize our data. Open Tableau and connect to your dataset.
First, drag the "Make" dimension to the Columns shelf and the "Year" dimension to the Rows shelf. Then, drag the "Sales" measure to the Marks card and change the mark type to "Bar".
Now that we have created our Tableau worksheet, we can export the data to Python.
Click on the "Worksheet" menu and select "Export" > "Data". In the dialog box that appears, select "Tabular Data" and choose "Python" as the format. Save the file as "car_sales.py" in a location of your choice.
Now that we have exported the data to Python, we can create a 2D data visualization using matplotlib.
import matplotlib.pyplot as plt
import car_sales
# Group the data by Make and Year
df = car_sales.df.groupby(['Make', 'Year'])['Sales'].sum().unstack()
# Plot the data
df.plot(kind='bar')
plt.title('Car Sales by Make and Year')
plt.xlabel('Make')
plt.ylabel('Sales')
plt.show()
This will produce a 2D data visualization that looks like this:
In this tutorial, we have learned how to use Python and Tableau together to create a 2D data visualization. By exporting the data to Python, we were able to create a visualization using matplotlib that allows us to see the sales of different car makes over time.