Graphics, Figures & TablesBar chart too many characters

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
fruitfrisje
Posts: 10
Joined: Wed Dec 05, 2012 12:26 pm

Bar chart too many characters

Post by fruitfrisje »

Hi,

I've created a bar chart from some data. There are 157 data lines, which also can been seen in the horizontal x-axis in the chart. It's looks not nice with that much data, is it possible that I can only show every 5 years instead of every year.

I've found this:
x post scale=2
but that only give data every two year. I would lik to see every year but only the axis should give for example every 5 years

Code: Select all

\documentclass[letterpaper]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[   ybar,
  xlabel=Character,
  ylabel=Number of Locks,
  xtick=data,
  xticklabels from table={my.dat}{Character},
  bar width=0.5pt,
  width=1*\textwidth,
  x tick label style={font=\tiny,align=right,rotate=90},
  x post scale=2,
  enlarge x limits=0.015,
  enlarge y limits=0.01,
]
\addplot table
[   x=X-Position,
  y=Probability
] 
{my.dat};
\end{axis}
\end{tikzpicture}
\end{document}
and the my.dat file is:

Code: Select all

Character   X-Position  Probability
1856 1 1
1857 2 0
1858 3 0
1859 4 0
1860 5 0
1861 6 0
1862 7 0
1863 8 2
1864 9 0
1865 10 0
1866 11 0
1867 12 0
1868 13 0
1869 14 0
1870 15 3
1871 16 0
1872 17 2
1873 18 0
1874 19 0
1875 20 0
1876 21 0
1877 22 0
1878 23 0
1879 24 0
1880 25 0
1881 26 0
1882 27 0
1883 28 0
1884 29 1
1885 30 1
1886 31 1
1887 32 0
1888 33 0
1889 34 1
1890 35 0
1891 36 1
1892 37 0
1893 38 0
1894 39 0
1895 40 0
1896 41 1
1897 42 0
1898 43 1
1899 44 0
1900 45 0
1901 46 0
1902 47 0
1903 48 0
1904 49 0
1905 50 0
1906 51 1
1907 52 0
1908 53 0
1909 54 0
1910 55 1
1911 56 0
1912 57 0
1913 58 0
1914 59 0
1915 60 0
1916 61 2
1917 62 0
1918 63 0
1919 64 0
1920 65 2
1921 66 1
1922 67 0
1923 68 1
1924 69 0
1925 70 1
1926 71 3
1927 72 2
1928 73 1
1929 74 0
1930 75 4
1931 76 4
1932 77 3
1933 78 4
1934 79 1
1935 80 1
1936 81 1
1937 82 8
1938 83 1
1939 84 0
1940 85 0
1941 86 0
1942 87 1
1943 88 0
1944 89 0
1945 90 0
1946 91 0
1947 92 0
1948 93 0
1949 94 0
1950 95 0
1951 96 0
1952 97 1
1953 98 2
1954 99 0
1955 100 1
1956 101 0
1957 102 0
1958 103 4
1959 104 1
1960 105 2
1961 106 1
1962 107 4
1963 108 0
1964 109 3
1965 110 0
1966 111 1
1967 112 5
1968 113 4
1969 114 4
1970 115 0
1971 116 0
1972 117 2
1973 118 1
1974 119 3
1975 120 1
1976 121 0
1977 122 1
1978 123 0
1979 124 0
1980 125 0
1981 126 0
1982 127 1
1983 128 2
1984 129 1
1985 130 0
1986 131 0
1987 132 5
1988 133 0
1989 134 1
1990 135 1
1991 136 1
1992 137 0
1993 138 0
1994 139 0
1995 140 0
1996 141 0
1997 142 1
1998 143 0
1999 144 0
2000 145 2
2001 146 0
2002 147 0
2003 148 2
2004 149 0
2005 150 0
2006 151 0
2007 152 2
2008 153 0
2009 154 3
2010 155 0
2011 156 0

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

Bar chart too many characters

Post by feuersaenger »

Hi,

The option 'xtick=data' combined with 'xticklabels from table' will take all labels per design.

The only alternative is to provide a suitable set of different tick position / tick label pairs. This could be done by means of

Code: Select all

	  xtick={5,15,...,170},
	  xticklabels={1860,1870,...,2020},
the first key will place tick *positions*. The second assigns suitable tick *labels*. The ... will generate suitable positions in-between (which need to match, of course).

However, why don't you simply plot your "Characters" column against the probability?

In fact, if you simply say

Code: Select all

    \addplot table
    [   
      x=Character,
      y=Probability
    ]
instead of your choice x=X-position and omit the two options xtick=data,xticklabels from table, you get the same output for free. And it would be much simpler to choose a different set of years. Note that this solution would require

Code: Select all

xticklabel style={/pgf/number format/1000 sep=},
because the default setting typesets tick labels using pgfmathprintnumber.

In addition, you should seriously consider using

Code: Select all

\pgfplotsset{compat=1.6}
in your preamble - this will automatically respect the size of tick labels for axis label placement.

The complete example would become

Code: Select all

    \documentclass[letterpaper]{article}
    \usepackage{tikz}
    \usepackage{pgfplots}
	
	\pgfplotsset{compat=1.6}

    \begin{document}
    \begin{tikzpicture}
    \begin{axis}
    [   ybar,
      xlabel=Character,
      ylabel=Number of Locks,
      %xtick=data,
      %xticklabels from table={my.dat}{Character},
	  %xtick={5,15,...,170},
	  %xticklabels={1860,1870,...,2020},
	  xticklabel style={/pgf/number format/1000 sep=},
      bar width=0.5pt,
      width=1*\textwidth,
      x tick label style={font=\tiny,align=right,rotate=90},
      x post scale=2,
      enlarge x limits=0.015,
      enlarge y limits=0.01,
    ]
    \addplot table
    [   
%		x=X-Position,
		x=Character,
      y=Probability
    ]
    {my.dat};
    \end{axis}
    \end{tikzpicture}
    \end{document}
fruitfrisje
Posts: 10
Joined: Wed Dec 05, 2012 12:26 pm

Bar chart too many characters

Post by fruitfrisje »

Hi,
I do get the first part

Code: Select all

          xtick={5,15,...,170},
          xticklabels={1860,1870,...,2020},
Second code is true... why do difficult if it can be easy :D

The part that i don't get is how i can adjust the number of character in the horizontal bar, or is this fixed?
feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

Bar chart too many characters

Post by feuersaenger »

Good that my answer helped so far.
The part that i don't get is how i can adjust the number of character in the horizontal bar, or is this fixed?
Unfortunately, I do not really understand your request... what precisely do you mean? Which characters? Which horizontal bar (I only see vertical bars)? Do you mean the number of tick labels (answer would be adjust the difference between the two numbers before ... , i.e. something like 5,10,...,20)? Or do you mean the number of minor ticks?
Post Reply