2

在 Y 轴(对数刻度)中,为什么 0-10 范围小于其他范围(10-100、100-1000 等)。有没有办法调整 x 刻度线和值的位置?我想清楚地显示小的值。在此处输入图像描述

word_freqs, words
([[7637.78430956, 1938.76578683, 208.902929772, 40.3146004823, 
120.943801447],
[6.99469414131, 46.9678505732, 51.2011611144, 0, 93.9478658318],
[3773.94093782, 188.697046891, 943.485234456, 849.13671101, 377.394093782]],
['energiestadt','energiepolitik','energieversorgung','energietag', 
'energiestrategie'])

我的脚本是参考

import pandas as pd
import matplotlib.pyplot as plt
raw_data = {'Words': words,
'energie_energiestadt': word_freqs[0],
'energie_march2017': word_freqs[1],
'energie_smartcity': word_freqs[2]}
df = pd.DataFrame(raw_data, columns = ['Words', 'energie_energiestadt', 
'energie_march2017', 'energie_smartcity'])
df

数据框

# Setting the positions and width for the bars
pos = list(range(len(df['energie_energiestadt'])))
width = 0.25

# Plotting the bars
fig, ax = plt.subplots(figsize=(10,5))

# Create a bar with energie_energiestadt data,
# in position pos,
plt.bar(pos,
        #using df['energie_energiestadt'] data,
        df['energie_energiestadt'],
        # of width
        width,
        # with alpha 0.5
        alpha=0.5,
        # with color
        color='#EE3224',
        # with label the first value in Words
        label=df['Words'][0])

# Create a bar with energie_march2017 data,
# in position pos + some width buffer,
plt.bar([p + width for p in pos],
        #using df['energie_march2017'] data,
        df['energie_march2017'],
        # of width
        width,
        # with alpha 0.5
        alpha=0.5,
        # with color
        color='#F78F2E',
        # with label the second value in Words
        label=df['Words'][1])

# Create a bar with energie_smartcity data,
# in position pos + some width buffer,
plt.bar([p + width*2 for p in pos],
        #using df['energie_smartcity'] data,
        df['energie_smartcity'],
        # of width
        width,
        # with alpha 0.5
        alpha=0.5,
        # with color
        color='#FFC222',
        # with label the third value in Words
        label=df['Words'][2], log=1)

# Set the y axis label
ax.set_ylabel('Frequency')

# Set the chart's title
ax.set_title('Frequency of words in different texts')

# Set the position of the x ticks
ax.set_xticks([p + 1.5 * width for p in pos])

# Set the labels for the x ticks
ax.set_xticklabels(df['Words'])

# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*4)
plt.ylim([0, max(df['energie_energiestadt'] + df['energie_march2017'] + 
df['energie_smartcity'])] )

# Adding the legend and showing the plot
plt.legend(['energie energiestadt', 'energie march2017', 'energie 
smartcity'], loc='upper right')
plt.grid()
plt.show()
4

1 回答 1

2

可以使用ax.set_ylim()或设置 y 轴的范围plt.ylim()。显然0不能是对数刻度的限制,因此您需要使用一些正数,例如ax.set_ylim((1e-1,None))

import matplotlib.pyplot as plt
import numpy as np


y = [7637.78,  1938.77,  208.9,  40.31,  120.94,  6.99,  46.97,  
     51.2,  0.0,  93.95,  3773.94,  188.7,  943.49,  849.14,  377.39]

y = np.array(y)

fig, ax = plt.subplots()
ax.bar(range(len(y)), y)
ax.set_yscale("log")
ax.set_ylim((1e-1,None))
plt.show()

在此处输入图像描述

于 2017-05-11T11:09:03.233 回答