Master LaTeX Bar Charts: Convert Existing Diagrams or Create New Ones with ChatGPT
This article is to convert an existent diagram or create a new latex bar chart diagram with ChatGPT. Also we are going to talk about the steps to generate latex code from diagrams of statista.com or ourworldindata and convert there diagram into a latex diagram.
Problem
The problem is I wanted to convert the following image into a latex diagram as a code segment:
Not to get copyright issues we removed all values and valuable text. The original can be found here:
https://ourworldindata.org/grapher/burden-of-disease-by-cause
Solution:
Steps to solve the problem.
Step 1 Get Raw Data:
Get the raw data of the chart. Therefore we have two options. We can download the CSV from the website or use a ORC tool. The last one would then convert the chart image from jpg to a text. Then we could take the text and format it into a text based list. For using the OCR method. I mostly like to use Tesseract or if you don’t want to install anything. You can use an online web solution. Here is on: https://www.onlineocr.net/
Step 2 Format Raw Data:
After we received our raw data we need to reformatted the text a little. Therefore we can use excel or sublime with search and replace. In the end my result looks something like this:
-Cardiovascular diseases 493,11 million
-Cancers is 451,39 million
-Neonatal disorders 285,89 million
-Other NCDs 253,17 million
-Respiratory infections and TB 253,05 million
-Musculoskeletal disorders 250,08 million
-Mental disorders 225,31 million
-Diabetes and kidney diseases 212,73 million
-Unintentional injuries 203,59 million
-Respiratory diseases 203,53 million
-Neurological disorders 197,72 million
-Enteric infections 196,84 million
-Digestive diseases 188,99 million
-Transport injuries 177,64 million
-Malaria & neglected tropical diseases 162,9 million
-HIV/AIDS and STIs 156,2 million
-Other infectious diseases 151,41 million
-Nutritional deficiencies 149,78 million
-Skin diseases 142,88 million
-Substance use disorders 135,13 million
-Self-harm 134,12 million
-Interpersonal violence 126,97 million
-Maternal disorders 112,68 million
-Conflict and terrorism 16,26 million
-Natural disasters 1,96087332 million
Please not that those values are not correct and that this values are only used for demonstration purposes.
Step 3 Create Diagram:
Use ChatGPT to create a latex diagram bar chart diagram. Therefore we are using the following prompt.
“The goal is to build a vertical bar diagram for latex. Start the diagram at 0. Also make the color of the bars with the hex code #38BDFF. The diagram should have the following content:
-Cardiovascular diseases 493,11 million
-Cancers is 451,39 million
-Neonatal disorders 285,89 million
-Other NCDs 253,17 million
-Respiratory infections and TB 253,05 million
-Musculoskeletal disorders 250,08 million
-Mental disorders 225,31 million
-Diabetes and kidney diseases 212,73 million
-Unintentional injuries 203,59 million
-Respiratory diseases 203,53 million
-Neurological disorders 197,72 million
-Enteric infections 196,84 million
-Digestive diseases 188,99 million
-Transport injuries 177,64 million
-Malaria & neglected tropical diseases 162,9 million
-HIV/AIDS and STIs 156,2 million
-Other infectious diseases 151,41 million
-Nutritional deficiencies 149,78 million
-Skin diseases 142,88 million
-Substance use disorders 135,13 million
-Self-harm 134,12 million
-Interpersonal violence 126,97 million
-Maternal disorders 112,68 million
-Conflict and terrorism 16,26 million
-Natural disasters 1,96087332 million ”
Result:
Result:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ xbar, width=15cm, height=20cm, enlarge y limits=0.05, symbolic y coords={ Cardiovascular diseases, Cancers, Neonatal disorders, Other NCDs, Respiratory infections and TB, Musculoskeletal disorders, Mental disorders, Diabetes and kidney diseases, Unintentional injuries, Respiratory diseases, Neurological disorders, Enteric infections, Digestive diseases, Transport injuries, Malaria \& neglected tropical diseases, HIV/AIDS and STIs, Other infectious diseases, Nutritional deficiencies, Skin diseases, Substance use disorders, Self-harm, Interpersonal violence, Maternal disorders, Conflict and terrorism, Natural disasters }, xlabel={Millions}, ytick=data, nodes near coords, nodes near coords align={horizontal}, bar width=10pt, xmin=0, bar shift=0pt, axis y line*=left, axis x line*=bottom, y axis line style={opacity=0}, y tick style={draw=none}, xmajorgrids, grid style={gray, dashed}, every node near coord/.append style={font=\small}, cycle list={fill=#38BDFF}]
\addplot coordinates {
(493.11,Cardiovascular diseases)
(451.39,Cancers)
(285.89,Neonatal disorders)
(253.17,Other NCDs)
(253.05,Respiratory infections and TB)
(250.08,Musculoskeletal disorders)
(225.31,Mental disorders)
(212.73,Diabetes and kidney diseases)
(203.59,Unintentional injuries)
(203.53,Respiratory diseases)
(197.72,Neurological disorders)
(196.84,Enteric infections)
(188.99,Digestive diseases)
(177.64,Transport injuries)
(162.9,Malaria \& neglected tropical diseases)
(156.2,HIV/AIDS and STIs)
(151.41,Other infectious diseases)
(149.78,Nutritional deficiencies)
(142.88,Skin diseases)
(135.13,Substance use disorders)
(134.12,Self-harm)
(126.97,Interpersonal violence)
(112.68,Maternal disorders)
(16.26,Conflict and terrorism)
(1.96087332,Natural disasters)
};
\end{axis}
\end{tikzpicture}
\end{document}
Step 4 View the diagram:
To see the result from ChatGPT we need to create a file and open our latex IDE to create a PDF. For the sack of simplicity we just used https://www.overleaf.com/project to quickly see the result.
Here is a screenshot:
Step 5 Final Diagram:
Now we are done and we can use the code. The final diagram looks like this:
Conclusion
I hope you liked the article. If you have any question don’t hesitate to ask. Happy coding.