पांडस बारप्लॉट में एक्स-अक्ष टिक लेबल को कैसे घुमाएं


99

निम्नलिखित कोड के साथ:

import matplotlib
matplotlib.style.use('ggplot')
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({ 'celltype':["foo","bar","qux","woz"], 's1':[5,9,1,7], 's2':[12,90,13,87]})
df = df[["celltype","s1","s2"]]
df.set_index(["celltype"],inplace=True)
df.plot(kind='bar',alpha=0.75)
plt.xlabel("")

मैंने यह साजिश रची:

यहां छवि विवरण दर्ज करें

मैं एक्स-अक्ष टिक लेबल को 0 डिग्री तक कैसे घुमा सकता हूं?

मैंने इसे जोड़ने की कोशिश की लेकिन काम नहीं किया:

plt.set_xticklabels(df.index,rotation=90)

जवाबों:


191

परम दर्रा rot=0xticks को घुमाने के लिए:

import matplotlib
matplotlib.style.use('ggplot')
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({ 'celltype':["foo","bar","qux","woz"], 's1':[5,9,1,7], 's2':[12,90,13,87]})
df = df[["celltype","s1","s2"]]
df.set_index(["celltype"],inplace=True)
df.plot(kind='bar',alpha=0.75, rot=0)
plt.xlabel("")
plt.show()

पैदावार की साजिश:

यहां छवि विवरण दर्ज करें



9

सवाल स्पष्ट है लेकिन शीर्षक उतना सटीक नहीं है जितना यह हो सकता है। मेरा जवाब उन लोगों के लिए है जो अक्ष लेबल को बदलने के लिए देख रहे हैं , टिक लेबल के विपरीत , जो कि स्वीकृत उत्तर के बारे में है। (शीर्षक अब सही कर दिया गया है)।

for ax in plt.gcf().axes:
    plt.sca(ax)
    plt.xlabel(ax.get_xlabel(), rotation=90)

8

आप set_xticklabels () का उपयोग कर सकते हैं

ax.set_xticklabels(df['Names'], rotation=90, ha='right')

3

निम्न सहायक हो सकते हैं:

# Valid font size are xx-small, x-small, small, medium, large, x-large, xx-large, larger, smaller, None

plt.xticks(
    rotation=45,
    horizontalalignment='right',
    fontweight='light',
    fontsize='medium',
)

उदाहरण और एपीआई के साथ यहां फ़ंक्शन xticks[संदर्भ] है

def xticks(ticks=None, labels=None, **kwargs):
    """
    Get or set the current tick locations and labels of the x-axis.

    Call signatures::

        locs, labels = xticks()            # Get locations and labels
        xticks(ticks, [labels], **kwargs)  # Set locations and labels

    Parameters
    ----------
    ticks : array_like
        A list of positions at which ticks should be placed. You can pass an
        empty list to disable xticks.

    labels : array_like, optional
        A list of explicit labels to place at the given *locs*.

    **kwargs
        :class:`.Text` properties can be used to control the appearance of
        the labels.

    Returns
    -------
    locs
        An array of label locations.
    labels
        A list of `.Text` objects.

    Notes
    -----
    Calling this function with no arguments (e.g. ``xticks()``) is the pyplot
    equivalent of calling `~.Axes.get_xticks` and `~.Axes.get_xticklabels` on
    the current axes.
    Calling this function with arguments is the pyplot equivalent of calling
    `~.Axes.set_xticks` and `~.Axes.set_xticklabels` on the current axes.

    Examples
    --------
    Get the current locations and labels:

        >>> locs, labels = xticks()

    Set label locations:

        >>> xticks(np.arange(0, 1, step=0.2))

    Set text labels:

        >>> xticks(np.arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue'))

    Set text labels and properties:

        >>> xticks(np.arange(12), calendar.month_name[1:13], rotation=20)

    Disable xticks:

        >>> xticks([])
    """

2

बार ग्राफ़ के लिए, आप उस कोण को शामिल कर सकते हैं, जिसे आप अंत में चाहते हैं कि टिक हो।

यहां मैं rot=0उन्हें एक्स अक्ष के समानांतर बनाने के लिए उपयोग कर रहा हूं ।

series.plot.bar(rot=0)
plt.show()
plt.close()

1
और हिस्टोग्राम के लिए, यह बहुत समान है। बदलें rotके साथ xrotयाyrot
टॉम
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.