<jupyter_start><jupyter_text>Create a bar plot in Pythonby **<NAME>**This is a small and fast example to create a bar plot with the data structure and data analysis tool of [pandas](https://pandas.pydata.org/). And it is a first try for me to design a small tuorial in [JupyterLab](https://github.com/jupyterlab). I assume here that Python, etc. is already installed. If not, check out the Python Distribution [Anaconda](https://www.anaconda.com/distribution/) and follow the install instructions there. Then you are ready to repeat the following lines of code and you should get the same results.   Import pandas and load a CSV fileImport the pandas package straightforward and rename it to pd. The only purpose behind the renaming is that one does not need to write the package's full name when calling specific functions.<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>As [pandas.read_csv()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html) will create an own index for an imported dataframe, we specify the index column as the grad_ID column of our dataset.<jupyter_code>df = pd.read_csv("Zvejnieki/dataset_Zvejnieki.csv", index_col = 'grab_ID')
df.head()<jupyter_output><empty_output><jupyter_text>Create a barplotPandas has inbuild functions to plot data from dataframes ([pandas.DataFrame.plot](https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.plot.html)). But beforehand we need to prepare the data a little more. In our case we would like to count the appearances of the biological sexes of the deceased in our dataset. Therefore, we use the [pandas.Series.value_counts](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.value_counts.html) function on the column 'biologisches_geschlecht' to do so.<jupyter_code>df['biologisches_geschlecht'].value_counts()<jupyter_output><empty_output><jupyter_text>Than we can plot directly.<jupyter_code>df['biologisches_geschlecht'].value_counts().plot(kind='bar')<jupyter_output><empty_output><jupyter_text>Plot the data (more respecable)The [pandas.DataFrame.plot](https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.plot.html) function has many arguments one can use to change the appearance of the data. We will use only some of them and you can find those an more in this [user guide](https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html).* figsize=(width, hight) determins the width and heigt of teh fiure in inch* edgecolor will draw a line around the bars, what sets a nice contrast* alpha sets the transparancy of the bars and the edgecolor* rot rotates the x-axis labels by a given angle* fontsize de-/increases the size of the x- and y-axis labels<jupyter_code>df['biologisches_geschlecht'].value_counts().plot(kind='bar', figsize=(10, 6), edgecolor = 'black', alpha = 0.75, rot = 45.0, fontsize = 12)<jupyter_output><empty_output><jupyter_text>As the labels m, w, etc. are not really meaningful we should change them to proper words. We will there for overwrite specific values of column cells by a new value. This can also be done in the CSV spreadsheet directly.<jupyter_code>df.loc[df.biologisches_geschlecht == 'unbest', 'biologisches_geschlecht'] = 'indifferent'
df.loc[df.biologisches_geschlecht == 'm', 'biologisches_geschlecht'] = 'male'
df.loc[df.biologisches_geschlecht == 'w', 'biologisches_geschlecht'] = 'female'
df.loc[df.biologisches_geschlecht == 'm?', 'biologisches_geschlecht'] = ' uncertain male'
df.loc[df.biologisches_geschlecht == 'w?', 'biologisches_geschlecht'] = 'uncertain female'<jupyter_output><empty_output><jupyter_text>And than plot again.<jupyter_code>df['biologisches_geschlecht'].value_counts().plot(kind='bar', figsize=(10, 6), edgecolor = 'black', alpha = 0.75, rot = 0.0, fontsize = 12)<jupyter_output><empty_output><jupyter_text>One last thing, to change the appearance of the plot in a fast way there is a convieniet option given by the package [matplotlib](https://matplotlib.org/). There are several plot styles which can be chosen from and will change the overall appearance of the plot.<jupyter_code>import matplotlib.pyplot as plt

plt.style.available<jupyter_output><empty_output><jupyter_text>We can call one of those styles and perform the plotting again to see what happens.<jupyter_code>plt.style.use('ggplot')

df['biologisches_geschlecht'].value_counts().plot(kind='bar', figsize=(10, 6), edgecolor = 'black', alpha = 0.75, rot = 0.0, fontsize = 12)<jupyter_output><empty_output><jupyter_text>Or another one.<jupyter_code>plt.style.use('dark_background')

df['biologisches_geschlecht'].value_counts().plot(kind='bar', figsize=(10, 6), edgecolor = 'black', alpha = 0.75, rot = 0.0, fontsize = 12)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10877266097103563, 'fraction_numerical': 0.010210460512606793, 'mean_word_length': 6.2180451127819545, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 9, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '241508', 'license': 'MIT', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1539, 'n_words': 470, 'path': 'bar_plot_python/bar_plot.ipynb', 'repo_name': 'archca/plot_gallery', 'size': '4805'}